Example usage for org.apache.commons.lang3 Validate validState

List of usage examples for org.apache.commons.lang3 Validate validState

Introduction

In this page you can find the example usage for org.apache.commons.lang3 Validate validState.

Prototype

public static void validState(final boolean expression) 

Source Link

Document

Validate that the stateful condition is true ; otherwise throwing an exception.

Usage

From source file:com.offbynull.voip.kademlia.model.InternalValidate.java

static void correctState(Node node, boolean condition) {
    // throws illegalstateexception, because if you made it to this point you should never encounter these conditions
    Validate.validState(node != null);

    if (!condition) {
        throw new BadNodeStateException(node);
    }// www .j  a v a  2 s  .co  m
}

From source file:com.offbynull.voip.kademlia.model.InternalValidate.java

static void exists(Node expectedNode, NodeLeastRecentSet nodeSet) {
    // throws illegalstateexception, because if you made it to this point you should never encounter these conditions
    Validate.validState(expectedNode != null);
    Validate.validState(nodeSet != null);

    Node node = nodeSet.get(expectedNode.getId());
    if (node == null) {
        throw new NodeNotFoundException(expectedNode);
    } else if (!node.getLink().equals(expectedNode.getLink())) {
        Validate.validState(node.getId().equals(expectedNode.getId())); // sanity check
        throw new LinkMismatchException(node, expectedNode.getLink());
    }/*from  w  w  w.j a v a 2 s  .  c  o m*/
}

From source file:com.offbynull.voip.kademlia.model.InternalValidate.java

static void matchesLength(int expectedLength, Id id) {
    // throws illegalstateexception, because if you made it to this point you should never encounter these conditions
    Validate.validState(id != null);
    Validate.validState(expectedLength > 0); // ids will always be 1 bit or greater

    // matching if the bitlengths match?
    if (expectedLength != id.getBitLength()) {
        throw new IdLengthMismatchException(id, expectedLength);
    }/*from   ww w  .  ja  v a2  s. com*/
}

From source file:com.offbynull.voip.kademlia.model.InternalValidate.java

static void forwardTime(Instant previousTime, Instant currentTime) {
    // throws illegalstateexception, because if you made it to this point you should never encounter these conditions
    Validate.validState(previousTime != null);
    Validate.validState(currentTime != null);

    if (currentTime.isBefore(previousTime)) {
        throw new BackwardTimeException(previousTime, currentTime);
    }/*from www. jav  a  2s  . c  o  m*/
}

From source file:de.vandermeer.asciitable.AT_Row.java

/**
 * Creates a new row representing a rule.
 * @param type the type for the rule row, must not be null nor {@link TableRowType#CONTENT} nor {@link TableRowType#UNKNOWN}
 * @param style the style for the rule row, must not be null nor {@link TableRowStyle#UNKNOWN}
 * @return a new row representing a rule
 * @throws {@link NullPointerException} if type or style where null
 * @throws {@link IllegalStateException} if type or style where unknown or if type was {@link TableRowType#CONTENT}
 *//*  ww  w .  j  a  v  a2s  .  c  o m*/
public static AT_Row createRule(TableRowType type, TableRowStyle style) {
    Validate.notNull(type);
    Validate.validState(type != TableRowType.UNKNOWN);
    Validate.validState(type != TableRowType.CONTENT);
    Validate.notNull(style);
    Validate.validState(style != TableRowStyle.UNKNOWN);

    return new AT_Row() {
        @Override
        public TableRowType getType() {
            return type;
        }

        @Override
        public TableRowStyle getStyle() {
            return style;
        }
    };
}

From source file:net.dstc.mkts.config.ServerSettingsImpl.java

@Override
public void setPort(@DecimalMin("1024") @DecimalMax("65535") int port) {
    Validate.validState(port >= 1024 && port < 644536);
    this.port = port;
}

From source file:com.github.mbenson.privileged.weaver.Body.java

Body complete() {
    try {
        return endBlock();
    } finally {
        Validate.validState(level == 0);
    }
}

From source file:com.offbynull.voip.kademlia.model.InternalValidate.java

static void matchesPrefix(BitString expectedPrefix, Id id) {
    // throws illegalstateexception, because if you made it to this point you should never encounter these conditions
    Validate.validState(id != null);
    Validate.validState(expectedPrefix != null);

    // matching if the bitlengths match?
    if (id.getBitString().getSharedPrefixLength(expectedPrefix) != expectedPrefix.getBitLength()) {
        throw new IdPrefixMismatchException(id, expectedPrefix);
    }//  w ww  . j  a  v  a2s  . c  om
}

From source file:de.vandermeer.skb.interfaces.application.ApplicationErrorCode.java

/**
 * Creates a new error message with all arguments substituted.
 * The length of `arguments` must be equal to the number of expected arguments of this error code.
 * If the number of expected arguments is 0, then `args` can be null or of length 0.
 * @param args the arguments, must have same length as expected arguments
 * @return new error message/*from ww w  .j a  v a 2s .c o m*/
 */
default String getMessage(final Object... args) {
    if (this.getArgs() > 0) {
        Validate.noNullElements(args);
        Validate.validState(args.length == this.getArgs());
    } else {
        Validate.validState(args == null || args.length == 0);
        return this.getMessage();
    }

    final StrBuilder ret = new StrBuilder().append(this.getMessage());
    for (final Object arg : args) {
        ret.replaceFirst("{}", arg.toString());
    }
    return ret.toString();
}

From source file:com.offbynull.portmapper.upnpigd.UpnpIgdPortMapper.java

@Override
public MappedPort mapPort(PortType portType, int internalPort, long lifetime) throws InterruptedException {
    Validate.validState(!closed);
    Validate.notNull(portType);//w w w. j av  a  2  s.  c om
    Validate.inclusiveBetween(1, 65535, internalPort);
    Validate.inclusiveBetween(1L, Long.MAX_VALUE, lifetime);

    int externalPort = random.nextInt(55535) + 10000; // 10000 - 65535
    PortMappingInfo info;
    InetAddress externalAddress;
    try {
        info = controller.addPortMapping(externalPort, internalPort, portType, lifetime);
        externalAddress = controller.getExternalIp();
    } catch (IllegalArgumentException | ResponseException re) {
        throw new IllegalStateException(re);
    }

    return new MappedPort(info.getInternalPort(), info.getExternalPort(), externalAddress, info.getPortType(),
            info.getRemainingDuration());
}