Java Code Examples for java.util.EnumSet#iterator()

The following examples show how to use java.util.EnumSet#iterator() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * @param caseSensitive  case-sensitive lookup for Enum label
 */
public EnumFactory(Class<E> enumClass, E defaultEnum, boolean caseSensitive)
{
    this.defaultEnum = defaultEnum;

    // setup label map
    labelMap = caseSensitive ? new HashMap<String, E>(10) : new TreeMap<String, E>(String.CASE_INSENSITIVE_ORDER);
    EnumSet<E> enumSet = EnumSet.allOf(enumClass);
    Iterator<E> iter = enumSet.iterator();
    while(iter.hasNext())
    {
        E e = iter.next();
        if (e instanceof EnumLabel)
        {
            labelMap.put(((EnumLabel)e).getLabel(), e);
        }
    }
}
 
Example 2
Source Project: hadoop   File: TestEnumSetWritable.java    License: Apache License 2.0 6 votes vote down vote up
/** 
 * test {@code EnumSetWritable.write(DataOutputBuffer out)} 
 *  and iteration by TestEnumSet through iterator().
 */
public void testEnumSetWritableWriteRead() throws Exception {
  EnumSetWritable<TestEnumSet> srcSet = new EnumSetWritable<TestEnumSet>(
      EnumSet.of(TestEnumSet.APPEND, TestEnumSet.CREATE), TestEnumSet.class);
  DataOutputBuffer out = new DataOutputBuffer();
  srcSet.write(out);

  EnumSetWritable<TestEnumSet> dstSet = new EnumSetWritable<TestEnumSet>();
  DataInputBuffer in = new DataInputBuffer();
  in.reset(out.getData(), out.getLength());
  dstSet.readFields(in);

  EnumSet<TestEnumSet> result = dstSet.get();
  Iterator<TestEnumSet> dstIter = result.iterator();
  Iterator<TestEnumSet> srcIter = srcSet.iterator();
  while (dstIter.hasNext() && srcIter.hasNext()) {
    assertEquals("testEnumSetWritableWriteRead error !!!", dstIter.next(),
        srcIter.next());
  }
}
 
Example 3
Source Project: big-c   File: TestEnumSetWritable.java    License: Apache License 2.0 6 votes vote down vote up
/** 
 * test {@code EnumSetWritable.write(DataOutputBuffer out)} 
 *  and iteration by TestEnumSet through iterator().
 */
public void testEnumSetWritableWriteRead() throws Exception {
  EnumSetWritable<TestEnumSet> srcSet = new EnumSetWritable<TestEnumSet>(
      EnumSet.of(TestEnumSet.APPEND, TestEnumSet.CREATE), TestEnumSet.class);
  DataOutputBuffer out = new DataOutputBuffer();
  srcSet.write(out);

  EnumSetWritable<TestEnumSet> dstSet = new EnumSetWritable<TestEnumSet>();
  DataInputBuffer in = new DataInputBuffer();
  in.reset(out.getData(), out.getLength());
  dstSet.readFields(in);

  EnumSet<TestEnumSet> result = dstSet.get();
  Iterator<TestEnumSet> dstIter = result.iterator();
  Iterator<TestEnumSet> srcIter = srcSet.iterator();
  while (dstIter.hasNext() && srcIter.hasNext()) {
    assertEquals("testEnumSetWritableWriteRead error !!!", dstIter.next(),
        srcIter.next());
  }
}
 
Example 4
Source Project: j2objc   File: EnumSetTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testRemoveIteratorRemoveFromHugeEnumSet() {
    EnumSet<HugeEnumCount> set = EnumSet.noneOf(HugeEnumCount.class);
    set.add(HugeEnumCount.NO64);
    set.add(HugeEnumCount.NO65);
    set.add(HugeEnumCount.NO128);
    Iterator<HugeEnumCount> iterator = set.iterator();
    assertTrue(iterator.hasNext());
    assertEquals(HugeEnumCount.NO64, iterator.next());
    assertTrue(iterator.hasNext());
    iterator.remove();
    assertEquals(HugeEnumCount.NO65, iterator.next());
    assertTrue(iterator.hasNext());
    assertEquals(HugeEnumCount.NO128, iterator.next());
    assertFalse(iterator.hasNext());
    assertEquals(EnumSet.of(HugeEnumCount.NO65, HugeEnumCount.NO128), set);
    iterator.remove();
    assertEquals(EnumSet.of(HugeEnumCount.NO65), set);
}
 
Example 5
private String joinEnumValues(EnumSet set) {
    boolean seenFirst = false;
    StringBuffer result = new StringBuffer();
    for (Iterator iterator = set.iterator(); iterator.hasNext();) {
        Enum value = (Enum) iterator.next();
        if (seenFirst) {
            result.append(',');
        } else {
            seenFirst = true;
        }
        result.append(value.name());
    }
    return result.toString();
}
 
Example 6
Source Project: hadoop   File: EnumSetParam.java    License: Apache License 2.0 5 votes vote down vote up
/** Convert an EnumSet to a string of comma separated values. */
public static <E extends Enum<E>> String toString(EnumSet<E> set) {
  if (set == null || set.isEmpty()) {
    return "";
  } else {
    final StringBuilder b = new StringBuilder();
    final Iterator<E> i = set.iterator();
    b.append(i.next());
    while (i.hasNext()) {
      b.append(',').append(i.next());
    }
    return b.toString();
  }
}
 
Example 7
Source Project: hadoop   File: EnumSetParam.java    License: Apache License 2.0 5 votes vote down vote up
/** Convert an EnumSet to a string of comma separated values. */
static <E extends Enum<E>> String toString(EnumSet<E> set) {
  if (set == null || set.isEmpty()) {
    return "";
  } else {
    final StringBuilder b = new StringBuilder();
    final Iterator<E> i = set.iterator();
    b.append(i.next());
    for(; i.hasNext(); ) {
      b.append(',').append(i.next());
    }
    return b.toString();
  }
}
 
Example 8
Source Project: hadoop   File: EnumSetWritable.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * reset the EnumSetWritable with specified
 * <tt>value</value> and <tt>elementType</tt>. If the <tt>value</tt> argument
 * is null or its size is zero, the <tt>elementType</tt> argument must not be
 * null. If the argument <tt>value</tt>'s size is bigger than zero, the
 * argument <tt>elementType</tt> is not be used.
 * 
 * @param value
 * @param elementType
 */
public void set(EnumSet<E> value, Class<E> elementType) {
  if ((value == null || value.size() == 0)
      && (this.elementType == null && elementType == null)) {
    throw new IllegalArgumentException(
        "The EnumSet argument is null, or is an empty set but with no elementType provided.");
  }
  this.value = value;
  if (value != null && value.size() > 0) {
    Iterator<E> iterator = value.iterator();
    this.elementType = iterator.next().getDeclaringClass();
  } else if (elementType != null) {
    this.elementType = elementType;
  }
}
 
Example 9
Source Project: big-c   File: EnumSetParam.java    License: Apache License 2.0 5 votes vote down vote up
/** Convert an EnumSet to a string of comma separated values. */
public static <E extends Enum<E>> String toString(EnumSet<E> set) {
  if (set == null || set.isEmpty()) {
    return "";
  } else {
    final StringBuilder b = new StringBuilder();
    final Iterator<E> i = set.iterator();
    b.append(i.next());
    while (i.hasNext()) {
      b.append(',').append(i.next());
    }
    return b.toString();
  }
}
 
Example 10
Source Project: big-c   File: EnumSetParam.java    License: Apache License 2.0 5 votes vote down vote up
/** Convert an EnumSet to a string of comma separated values. */
static <E extends Enum<E>> String toString(EnumSet<E> set) {
  if (set == null || set.isEmpty()) {
    return "";
  } else {
    final StringBuilder b = new StringBuilder();
    final Iterator<E> i = set.iterator();
    b.append(i.next());
    for(; i.hasNext(); ) {
      b.append(',').append(i.next());
    }
    return b.toString();
  }
}
 
Example 11
Source Project: big-c   File: EnumSetWritable.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * reset the EnumSetWritable with specified
 * <tt>value</value> and <tt>elementType</tt>. If the <tt>value</tt> argument
 * is null or its size is zero, the <tt>elementType</tt> argument must not be
 * null. If the argument <tt>value</tt>'s size is bigger than zero, the
 * argument <tt>elementType</tt> is not be used.
 * 
 * @param value
 * @param elementType
 */
public void set(EnumSet<E> value, Class<E> elementType) {
  if ((value == null || value.size() == 0)
      && (this.elementType == null && elementType == null)) {
    throw new IllegalArgumentException(
        "The EnumSet argument is null, or is an empty set but with no elementType provided.");
  }
  this.value = value;
  if (value != null && value.size() > 0) {
    Iterator<E> iterator = value.iterator();
    this.elementType = iterator.next().getDeclaringClass();
  } else if (elementType != null) {
    this.elementType = elementType;
  }
}
 
Example 12
public static long getSlotIndexForMutationContextPreparedStatement(
    final EnumSet<AbstractUpsertOutputOperator.OperationContext> context)
{
  Iterator<AbstractUpsertOutputOperator.OperationContext> itrForContexts = context.iterator();
  long indexValue = 0;
  while (itrForContexts.hasNext()) {
    AbstractUpsertOutputOperator.OperationContext aContext = itrForContexts.next();
    indexValue += Math.pow(10, aContext.ordinal());
  }
  return indexValue;
}
 
Example 13
private static Set<Flag> flagsFor(byte b) {
    EnumSet<Flag> s = EnumSet.allOf(Flag.class);

    Iterator<Flag> iter = s.iterator();

    while (iter.hasNext()) {
        int mask2 = iter.next().mask;
        if ((mask2 & b) != mask2) {
            iter.remove();
        }
    }

    return s;
}
 
Example 14
public static String enumSetToString(EnumSet<ArticleChangeType> enumSet) {
    if(enumSet.isEmpty()) return "";
    if(enumSet.size() == 1) return enumSet.iterator().next().name();

    Iterator<ArticleChangeType> it = enumSet.iterator();
    StringBuilder sb = new StringBuilder(it.next().name());
    while(it.hasNext()) {
        sb.append(STRING_DELIMITER).append(it.next().name());
    }

    return sb.toString();
}