예제 #1
0
  /**
   * Validates expected {@link org.terracotta.statistics.OperationStatistic} updates for the
   * indicated {@code Ehcache} instance. The statistics identified in {@code changed} are checked
   * for a value of {@code 1}; all other statistics in the same enumeration class are checked for a
   * value of {@code 0}.
   *
   * @param ehcache the {@code Ehcache} instance to check
   * @param changed the statistics values that should have updated values
   * @param <E> the statistics enumeration type
   */
  protected static <E extends Enum<E>> void validateStats(
      final Ehcache<?, ?> ehcache, final EnumSet<E> changed) {
    assert changed != null;
    final EnumSet<E> unchanged = EnumSet.complementOf(changed);

    @SuppressWarnings("unchecked")
    final List<EnumSet<E>> sets = Arrays.asList(changed, unchanged);
    Class<E> statsClass = null;
    for (final EnumSet<E> set : sets) {
      if (!set.isEmpty()) {
        statsClass = set.iterator().next().getDeclaringClass();
        break;
      }
    }
    assert statsClass != null;

    final OperationStatistic<E> operationStatistic = getOperationStatistic(ehcache, statsClass);
    for (final E statId : changed) {
      assertThat(
          String.format("Value for %s.%s", statId.getDeclaringClass().getName(), statId.name()),
          getStatistic(operationStatistic, statId),
          StatisticMatcher.equalTo(1L));
    }
    for (final E statId : unchanged) {
      assertThat(
          String.format("Value for %s.%s", statId.getDeclaringClass().getName(), statId.name()),
          getStatistic(operationStatistic, statId),
          StatisticMatcher.equalTo(0L));
    }
  }
예제 #2
0
  public static <E extends Enum<E>> E valueOf(Class<E> c, String name, boolean ignorecase) {
    // trim any leading or trailing spaces from the name
    name = name.trim();

    if (!ignorecase) {
      {
        return Enum.<E>valueOf(c, name);
      }
    }

    E[] universe = c.getEnumConstants();
    if (universe == null) {
      throw new IllegalArgumentException(name + " is not an enum type");
    }

    Map<String, E> map = cacheEnumValuesInCapitalLetters.get(c);

    if (map == null) {
      // populate the map with enum values and add it to cache
      map = new HashMap<String, E>(2 * universe.length);

      for (E e : universe) {
        map.put(e.name().toUpperCase(), e);
      }
      cacheEnumValuesInCapitalLetters.put(c, map);
    }

    E result = map.get(name.toUpperCase());
    if (result == null) {
      throw new IllegalArgumentException("No enum const " + c.getName() + "." + name);
    }
    return result;
  }
예제 #3
0
  public EnumBytesMarshaller(@NotNull Class<E> classMarshaled, E defaultValue) {
    this.defaultValue = defaultValue;

    mask = interner.length - 1;
    for (E e : classMarshaled.getEnumConstants()) {
      map.put(e.name(), e);
      int idx = hashFor(e.name());
      if (!internerDup.get(idx)) {
        if (interner[idx] != null) {
          //noinspection UnqualifiedFieldAccess,AssignmentToNull
          interner[idx] = null;
          internerDup.set(idx);

        } else {
          interner[idx] = e;
        }
      }
    }
  }
 @SuppressWarnings({"unchecked", "rawtypes"})
 public FieldsValidatingForm(final Class<F> fields, final Class<E> errors, final Editor editor) {
   this.fields = fields;
   this.errors = errors;
   this.editor = editor;
   for (final E err : errors.getEnumConstants()) {
     final F field = err.getField();
     if (editor.contains(field)) {
       addValidation(
           field.name(), err.name(), (HasValue) editor.getEditor(field), err.getValidator());
     }
   }
 }
  /**
   * Constructs a new {@code EnumParam}, initialized to the default value. Limit the set of options
   * with the supplied {@code EnumSet}.
   *
   * @param name of the {@code Param} (max 72 chars)
   * @param info {@code String} for use in tooltips; a {@code null} value will be set to an empty
   *     {@code String} (max 256 chars)
   * @param defaultValue of the {@code Param}
   * @param options a subset of the option type
   * @throws IllegalArgumentException if {@code name} is an empty string, {@code options} is empty,
   *     or the {@code defaultValue} is not included in {@code options}
   */
  DefaultEnumParam(String name, String info, E defaultValue, Set<E> options) {
    super(name, info, defaultValue);
    checkNotNull(options);
    checkArgument(!options.isEmpty(), "Options is empty");
    checkArgument(options.contains(defaultValue), "Default missing from options");
    this.options = Sets.immutableEnumSet(options);

    // init state
    state.addProperty(TYPE.toString(), ENUM.toString());
    JsonArray stateOptions = new JsonArray();
    for (E option : options) {
      stateOptions.add(new JsonPrimitive(option.name()));
    }
    state.add(OPTIONS.toString(), stateOptions);
  }
예제 #6
0
 public <E extends Composite> E parse(String textString, Class<E> composite) {
   E e;
   try {
     Pattern pattern;
     Matcher matcher;
     e = composite.newInstance();
     Class component;
     switch (e.name()) {
       case "Word":
         component = Word.class.getClass();
         e = composite.newInstance();
         pattern = Pattern.compile("(\\S*[\\s])");
         break;
       case "Symbol":
         component = Symbol.class.getClass();
         e = composite.newInstance();
         pattern = Pattern.compile("[\\S*]");
         break;
       default:
         component = Word.class.getClass();
         pattern = Pattern.compile("[\\S*]");
     }
     matcher = pattern.matcher(textString);
     while (matcher.find()) {
       if (component == Symbol.class) {
         Symbol s = new Symbol(matcher.group());
         e.add(s);
         System.out.print(s);
       } else {
         Component c = this.parse(matcher.group(), component);
         e.add(c);
         System.out.print(c);
       }
     }
     return e;
   } catch (InstantiationException | IllegalAccessException ignored) {
     throw new RuntimeException();
   }
 }
 protected <E extends Enum<E>> String renderEnum(E argument) {
   return '\'' + argument.name() + '\'';
 }
예제 #8
0
 @Override
 public void write(@NotNull Bytes bytes, @Nullable E e) {
   bytes.writeUTFΔ(e == null ? "" : e.name());
 }
예제 #9
0
 protected String getGroupToken(E group) {
   return group.name();
 }