Coverage Summary for Class: JsonRpcIdentifiableMessage (co.rsk.jsonrpc)

Class Class, % Method, % Line, %
JsonRpcIdentifiableMessage 0% (0/1) 0% (0/3) 0% (0/7)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2018 RSK Labs Ltd. 4  * 5  * This program is free software: you can redistribute it and/or modify 6  * it under the terms of the GNU Lesser General Public License as published by 7  * the Free Software Foundation, either version 3 of the License, or 8  * (at your option) any later version. 9  * 10  * This program is distributed in the hope that it will be useful, 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13  * GNU Lesser General Public License for more details. 14  * 15  * You should have received a copy of the GNU Lesser General Public License 16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 17  */ 18 package co.rsk.jsonrpc; 19  20 import com.fasterxml.jackson.annotation.JsonInclude; 21  22 /** 23  * The basic JSON-RPC request or response. It is required to have an ID. 24  * 25  * Note that the JSON-RPC 2.0 spec allows using strings as IDs, but our implementation doesn't. 26  */ 27 public abstract class JsonRpcIdentifiableMessage extends JsonRpcMessage { 28  private final int id; 29  30  public JsonRpcIdentifiableMessage(JsonRpcVersion version, int id) { 31  super(version); 32  this.id = requireNonNegative(id); 33  } 34  35  @JsonInclude(JsonInclude.Include.ALWAYS) 36  public int getId() { 37  return id; 38  } 39  40  private static int requireNonNegative(int id) { 41  if (id < 0) { 42  throw new IllegalArgumentException( 43  String.format("JSON-RPC message id should be a positive number, but was %s.", id) 44  ); 45  } 46  47  return id; 48  } 49 }