org.apache.commons.lang.Validate.isTrue()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(300)

本文整理了Java中org.apache.commons.lang.Validate.isTrue()方法的一些代码示例,展示了Validate.isTrue()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Validate.isTrue()方法的具体详情如下:
包路径:org.apache.commons.lang.Validate
类名称:Validate
方法名:isTrue

Validate.isTrue介绍

[英]Validate that the argument condition is true; otherwise throwing an exception. This method is useful when validating according to an arbitrary boolean expression, such as validating a primitive number or using your own custom validation expression.

Validate.isTrue(i > 0); 
Validate.isTrue(myObject.isOk());

The message of the exception is "The validated expression is false".
[中]验证参数条件是否为[$0$];否则将引发异常。当根据任意布尔表达式进行验证时,此方法非常有用,例如验证基元数或使用自己的自定义验证表达式。

Validate.isTrue(i > 0); 
Validate.isTrue(myObject.isOk());

异常的消息是“已验证的表达式为false”。

代码示例

代码示例来源:origin: Bukkit/Bukkit

/**
 * Creates a new note.
 *
 * @param note Internal note id. {@link #getId()} always return this
 *     value. The value has to be in the interval [0; 24].
 */
public Note(int note) {
  Validate.isTrue(note >= 0 && note <= 24, "The note value has to be between 0 and 24.");
  this.note = (byte) note;
}

代码示例来源:origin: Bukkit/Bukkit

private Color(int red, int green, int blue) {
  Validate.isTrue(red >= 0 && red <= BIT_MASK, "Red is not between 0-255: ", red);
  Validate.isTrue(green >= 0 && green <= BIT_MASK, "Green is not between 0-255: ", green);
  Validate.isTrue(blue >= 0 && blue <= BIT_MASK, "Blue is not between 0-255: ", blue);
  this.red = (byte) red;
  this.green = (byte) green;
  this.blue = (byte) blue;
}

代码示例来源:origin: Bukkit/Bukkit

public ServerListPingEvent(final InetAddress address, final String motd, final int numPlayers, final int maxPlayers) {
  Validate.isTrue(numPlayers >= 0, "Cannot have negative number of players online", numPlayers);
  this.address = address;
  this.motd = motd;
  this.numPlayers = numPlayers;
  this.maxPlayers = maxPlayers;
}

代码示例来源:origin: Bukkit/Bukkit

public Mushroom(Material shroom) {
  super(shroom);
  Validate.isTrue(shroom == Material.HUGE_MUSHROOM_1 || shroom == Material.HUGE_MUSHROOM_2, "Not a mushroom!");
}

代码示例来源:origin: Bukkit/Bukkit

/**
 *
 * @deprecated Magic value
 */
@Deprecated
public Mushroom(Material shroom, byte data) {
  super(shroom, data);
  Validate.isTrue(shroom == Material.HUGE_MUSHROOM_1 || shroom == Material.HUGE_MUSHROOM_2, "Not a mushroom!");
}

代码示例来源:origin: Bukkit/Bukkit

/**
 * Gets the color represented by the specified color code
 *
 * @param code Code to check
 * @return Associative {@link org.bukkit.ChatColor} with the given code,
 *     or null if it doesn't exist
 */
public static ChatColor getByChar(String code) {
  Validate.notNull(code, "Code cannot be null");
  Validate.isTrue(code.length() > 0, "Code must have at least one char");
  return BY_CHAR.get(code.charAt(0));
}

代码示例来源:origin: citerus/dddsample-core

/**
 * Constructor.
 *
 * @param countryAndLocation Location string.
 */
public UnLocode(final String countryAndLocation) {
 Validate.notNull(countryAndLocation, "Country and location may not be null");
 Validate.isTrue(VALID_PATTERN.matcher(countryAndLocation).matches(),
  countryAndLocation + " is not a valid UN/LOCODE (does not match pattern)");
 this.unlocode = countryAndLocation.toUpperCase();
}

代码示例来源:origin: Bukkit/Bukkit

public EntityDamageEvent(final Entity damagee, final DamageCause cause, final Map<DamageModifier, Double> modifiers, final Map<DamageModifier, ? extends Function<? super Double, Double>> modifierFunctions) {
  super(damagee);
  Validate.isTrue(modifiers.containsKey(DamageModifier.BASE), "BASE DamageModifier missing");
  Validate.isTrue(!modifiers.containsKey(null), "Cannot have null DamageModifier");
  Validate.noNullElements(modifiers.values(), "Cannot have null modifier values");
  Validate.isTrue(modifiers.keySet().equals(modifierFunctions.keySet()), "Must have a modifier function for each DamageModifier");
  Validate.noNullElements(modifierFunctions.values(), "Cannot have null modifier function");
  this.originals = new EnumMap<DamageModifier, Double>(modifiers);
  this.cause = cause;
  this.modifiers = modifiers;
  this.modifierFunctions = modifierFunctions;
}

代码示例来源:origin: Bukkit/Bukkit

/**
 * @return The note a semitone below this one.
 */
public Note flattened() {
  Validate.isTrue(note > 0, "This note cannot be flattened because it is the lowest known note!");
  return new Note(note - 1);
}

代码示例来源:origin: Bukkit/Bukkit

/**
 * Set whether this potion has extended duration. This will cause the
 * potion to have roughly 8/3 more duration than a regular potion.
 *
 * @param isExtended Whether the potion should have extended duration
 */
public void setHasExtendedDuration(boolean isExtended) {
  Validate.isTrue(type == null || !type.isInstant(), "Instant potions cannot be extended");
  extended = isExtended;
}

代码示例来源:origin: Bukkit/Bukkit

/**
 * Creates a new note for a natural tone, such as A-natural.
 *
 * @param octave The octave where the note is in. Has to be 0 - 1.
 * @param tone The tone within the octave.
 * @return The new note.
 */
public static Note natural(int octave, Tone tone) {
  Validate.isTrue(octave != 2, "Octave cannot be 2 for naturals");
  return new Note(octave, tone, false);
}

代码示例来源:origin: Bukkit/Bukkit

/**
 * @return The note a semitone above this one.
 */
public Note sharped() {
  Validate.isTrue(note < 24, "This note cannot be sharped because it is the highest known note!");
  return new Note(note + 1);
}

代码示例来源:origin: Bukkit/Bukkit

synchronized void initialize(JavaPlugin javaPlugin) {
    Validate.notNull(javaPlugin, "Initializing plugin cannot be null");
    Validate.isTrue(javaPlugin.getClass().getClassLoader() == this, "Cannot initialize plugin outside of this class loader");
    if (this.plugin != null || this.pluginInit != null) {
      throw new IllegalArgumentException("Plugin already initialized!", pluginState);
    }

    pluginState = new IllegalStateException("Initial initialization");
    this.pluginInit = javaPlugin;

    javaPlugin.init(loader, loader.server, description, dataFolder, file, this);
  }
}

代码示例来源:origin: Bukkit/Bukkit

/**
 * Sets the level of this potion.
 *
 * @param level The new level of this potion
 */
public void setLevel(int level) {
  Validate.notNull(this.type, "No-effect potions don't have a level.");
  int max = type.getMaxLevel();
  Validate.isTrue(level > 0 && level <= max, "Level must be " + (max == 1 ? "" : "between 1 and ") + max + " for this potion");
  this.level = level;
}

代码示例来源:origin: Bukkit/Bukkit

/**
 * Creates a new color object from an integer that contains the red,
 * green, and blue bytes in the lowest order 24 bits.
 *
 * @param rgb the integer storing the red, green, and blue values
 * @return a new color object for specified values
 * @throws IllegalArgumentException if any data is in the highest order 8
 *     bits
 */
public static Color fromRGB(int rgb) throws IllegalArgumentException {
  Validate.isTrue((rgb >> 24) == 0, "Extrenuous data in: ", rgb);
  return fromRGB(rgb >> 16 & BIT_MASK, rgb >> 8 & BIT_MASK, rgb >> 0 & BIT_MASK);
}

代码示例来源:origin: Bukkit/Bukkit

public PlayerEditBookEvent(Player who, int slot, BookMeta previousBookMeta, BookMeta newBookMeta, boolean isSigning) {
  super(who);
  Validate.isTrue(slot >= 0 && slot <=8, "Slot must be in range 0-8 inclusive");
  Validate.notNull(previousBookMeta, "Previous book meta must not be null");
  Validate.notNull(newBookMeta, "New book meta must not be null");
  Bukkit.getItemFactory().equals(previousBookMeta, newBookMeta);
  this.previousBookMeta = previousBookMeta;
  this.newBookMeta = newBookMeta;
  this.slot = slot;
  this.isSigning = isSigning;
  this.cancel = false;
}

代码示例来源:origin: Bukkit/Bukkit

/**
 *
 * @deprecated Magic value
 */
@Deprecated
public Mushroom(int type, byte data){
  super(type, data);
  Validate.isTrue(type == Material.HUGE_MUSHROOM_1.getId() || type == Material.HUGE_MUSHROOM_2.getId(), "Not a mushroom!");
}

代码示例来源:origin: apache/storm

private long nextTime(KafkaSpoutMessageId msgId) {
  Validate.isTrue(msgId.numFails() > 0, "nextTime assumes the message has failed at least once");
  final long currentTimeNanos = Time.nanoTime();
  final long nextTimeNanos = msgId.numFails() == 1                // numFails = 1, 2, 3, ...
      ? currentTimeNanos + initialDelay.lengthNanos
      : currentTimeNanos + delayPeriod.lengthNanos * (long)(Math.pow(2, msgId.numFails() - 1));
  return Math.min(nextTimeNanos, currentTimeNanos + maxDelay.lengthNanos);
}

代码示例来源:origin: Bukkit/Bukkit

/**
 * Applies the effects of this potion to the given {@link ItemStack}. The
 * ItemStack must be a potion.
 *
 * @param to The itemstack to apply to
 */
public void apply(ItemStack to) {
  Validate.notNull(to, "itemstack cannot be null");
  Validate.isTrue(to.getType() == Material.POTION, "given itemstack is not a potion");
  to.setDurability(toDamageValue());
}

代码示例来源:origin: Bukkit/Bukkit

/**
 * Creates a new note for a flat tone, such as A-flat.
 *
 * @param octave The octave where the note is in. Has to be 0 - 1.
 * @param tone The tone within the octave.
 * @return The new note.
 */
public static Note flat(int octave, Tone tone) {
  Validate.isTrue(octave != 2, "Octave cannot be 2 for flats");
  tone = tone == Tone.G ? Tone.F : Tone.values()[tone.ordinal() - 1];
  return new Note(octave, tone, tone.isSharpable());
}

相关文章

微信公众号

最新文章

更多