Example usage for java.util Set getClass

List of usage examples for java.util Set getClass

Introduction

In this page you can find the example usage for java.util Set getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:MainClass.java

public static void test(Set s) {
    System.out.println(s.getClass().getName().replaceAll("\\w+\\.", ""));
    fill(s);//w  w w  .  j  a v  a2  s .  c o  m
    fill(s);
    fill(s);
    System.out.println(s); // No duplicates!

    s.addAll(s);
    s.add("one");
    s.add("one");
    s.add("one");
    System.out.println(s);

    System.out.println("s.contains(\"one\"): " + s.contains("one"));
}

From source file:Set1.java

public static void test(Set s) {
    // Strip qualifiers from class name:
    System.out.println(s.getClass().getName().replaceAll("\\w+\\.", ""));
    fill(s);/*from   www  .  j a v  a 2 s.c  om*/
    fill(s);
    fill(s);
    System.out.println(s); // No duplicates!
    // Add another set to this one:
    s.addAll(s);
    s.add("one");
    s.add("one");
    s.add("one");
    System.out.println(s);
    // Look something up:
    System.out.println("s.contains(\"one\"): " + s.contains("one"));
}

From source file:Main.java

/**
 * Returns an unmodifiable versions of the Set of Enums.  If the contents of the set are known
 * to be unmodifiable by the caller in any way, the set itself will be retured, otherwise an
 * unmodifiable copy of the Set will be returned.
 * @param s Set to get the tamper-proof version of
 * @return An unmodifiable tamper-proof version of the set
 *//*from   www  .j  ava 2s . c  om*/
public static <E extends Enum<E>> Set<E> unmodifiableCopyOfEnumSet(Set<E> s) {
    Class<? extends Set> copyClass = s.getClass();

    if ((_EMPTY_SET == copyClass) || (_SINGLETON_SET == copyClass)) {
        // these classes are already unmodifiable, so just return
        return s;
    } else {
        return Collections.unmodifiableSet(EnumSet.copyOf(s));
    }
}

From source file:com.adobe.acs.commons.util.impl.DelegatingServletFactoryImpl.java

/**
 * Retrieves the delegation history from the Request
 *
 * @param request/*from  w w w.  jav a2s.com*/
 * @return the delegation history set (of resource types previously targeted by this Servlet)
 */
@SuppressWarnings("unchecked")
private Set<String> getDelegationHistory(final SlingHttpServletRequest request) {
    Set<String> history = new HashSet<String>();
    final Object tmp = request.getAttribute(REQUEST_ATTR_DELEGATION_HISTORY);
    if (history.getClass().isInstance(tmp)) {
        return (Set<String>) tmp;
    } else {
        return history;
    }
}

From source file:de.topicmapslab.kuria.runtime.util.TypeUtilTest.java

@SuppressWarnings("unchecked")
@Test/*w  w w. ja v  a 2  s.co m*/
public void testGetContainerType() {
    String[] tmp3 = new String[10];
    assertEquals("Check String array: ", String.class, TypeUtil.getContainerType(tmp3.getClass()));

    String tmp4[] = new String[10];
    assertEquals("Check String array: ", String.class, TypeUtil.getContainerType(tmp4.getClass()));

    try {
        Field field = TypeUtilTest.class.getDeclaredField("stringList");
        assertEquals("Check List: ", String.class, TypeUtil.getContainerType(field.getGenericType()));
        assertTrue("Check Vector: ", TypeUtil.isList(field.getGenericType()));

        field = TypeUtilTest.class.getDeclaredField("stringList2");
        assertEquals("Check ArrayListV: ", String.class, TypeUtil.getContainerType(field.getGenericType()));
        assertTrue("Check Vector: ", TypeUtil.isList(field.getGenericType()));

        field = TypeUtilTest.class.getDeclaredField("stringList3");
        assertEquals("Check Vector: ", String.class, TypeUtil.getContainerType(field.getGenericType()));
        assertTrue("Check Vector: ", TypeUtil.isList(field.getGenericType()));

        field = TypeUtilTest.class.getDeclaredField("stack");
        assertEquals("Check Stack: ", Object.class, TypeUtil.getContainerType(field.getGenericType()));

        field = TypeUtilTest.class.getDeclaredField("set1");
        assertEquals("Check Sets Parameter: ", Integer.class,
                TypeUtil.getContainerType(field.getGenericType()));
        assertTrue("Check Set: ", TypeUtil.isSet(field.getGenericType()));

        field = TypeUtilTest.class.getDeclaredField("set2");
        assertEquals("Check HashSet: ", Integer.class, TypeUtil.getContainerType(field.getGenericType()));
        assertTrue("Check HashSet: ", TypeUtil.isSet(field.getGenericType()));

        field = TypeUtilTest.class.getDeclaredField("set3");
        assertEquals("Check untyped Set: ", Object.class, TypeUtil.getContainerType(field.getGenericType()));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    // expecting object because no real reflection possible on var types.
    List<String> tmp = new ArrayList<String>();
    assertEquals("Check ArrayList: ", Object.class, TypeUtil.getContainerType(tmp.getClass()));
    tmp = new ArrayStack();
    assertEquals("Check ArrayList: ", Object.class, TypeUtil.getContainerType(tmp.getClass()));
    tmp = new Vector<String>();
    assertEquals("Check ArrayList: ", Object.class, TypeUtil.getContainerType(tmp.getClass()));

    Set<Integer> tmp2 = new HashSet<Integer>();
    assertEquals("Check HashSet: ", Object.class, TypeUtil.getContainerType(tmp2.getClass()));

    try {
        TypeUtil.getContainerType(String.class);
    } catch (IllegalArgumentException e) {
        return;
    }
    fail("No exception thrown");
}

From source file:de.javakaffee.kryoserializers.KryoTest.java

@Test(enabled = true)
public void testSingletonSet() throws Exception {
    final Set<?> obj = Collections.singleton("foo");
    final Set<?> deserialized = deserialize(serialize(obj), obj.getClass());
    assertDeepEquals(deserialized, obj);
}

From source file:com.transwarp.hbase.bulkload.TextSortReducer.java

@Override
protected void reduce(ImmutableBytesWritable rowKey, java.lang.Iterable<Text> lines,
        Reducer<ImmutableBytesWritable, Text, ImmutableBytesWritable, KeyValue>.Context context)
        throws java.io.IOException, InterruptedException {
    // although reduce() is called per-row, handle pathological case
    long threshold = context.getConfiguration().getLong("reducer.row.threshold", 1L * (1 << 30));
    Iterator<Text> iter = lines.iterator();
    while (iter.hasNext()) {
        Set<KeyValue> map = new TreeSet<KeyValue>(KeyValue.COMPARATOR);
        long curSize = 0;
        // stop at the end or the RAM threshold
        while (iter.hasNext() && curSize < threshold) {
            Text line = iter.next();
            String lineStr = line.toString();
            try {
                ArrayList<String> parsedLine = ParsedLine.parse(converter.getRecordSpec(), lineStr);
                Put p = converter.convert(parsedLine, rowKey.get());

                for (List<KeyValue> kvs : p.getFamilyMap().values()) {
                    for (KeyValue kv : kvs) {
                        map.add(kv);//from   w  w w  .j  a  v a  2s  . c o  m
                        curSize += kv.getLength();
                    }
                }
            } catch (FormatException badLine) {
                if (skipBadLines) {
                    System.err.println("Bad line." + badLine.getMessage());
                    incrementBadLineCount(1);
                    return;
                }
                throw new IOException(badLine);
            } catch (IllegalArgumentException e) {
                if (skipBadLines) {
                    System.err.println("Bad line." + e.getMessage());
                    incrementBadLineCount(1);
                    return;
                }
                throw new IOException(e);
            }
        }
        context.setStatus("Read " + map.size() + " entries of " + map.getClass() + "("
                + StringUtils.humanReadableInt(curSize) + ")");
        int index = 0;
        for (KeyValue kv : map) {
            if (isDelete) {
                kv = new KeyValue(kv.getRow(), kv.getFamily(), kv.getQualifier(), 0, KeyValue.Type.Delete,
                        kv.getValue());
            }
            context.write(rowKey, kv);
            if (++index > 0 && index % 100 == 0)
                context.setStatus("Wrote " + index + " key values.");
        }

        // if we have more entries to process
        if (iter.hasNext()) {
            // force flush because we cannot guarantee intra-row sorted order
            context.write(null, null);
        }
    }
}

From source file:com.transwarp.hbase.bulkload.withindex.TextWithIndexSortReducer.java

@Override
protected void reduce(ImmutableBytesWritable rowKey, java.lang.Iterable<Text> lines,
        Reducer<ImmutableBytesWritable, Text, ImmutableBytesWritable, KeyValue>.Context context)
        throws java.io.IOException, InterruptedException {
    // although reduce() is called per-row, handle pathological case
    long threshold = context.getConfiguration().getLong("reducer.row.threshold", 1L * (1 << 30));
    Iterator<Text> iter = lines.iterator();
    boolean qualifier = context.getConfiguration().getBoolean("indexqualifier", false);
    while (iter.hasNext()) {
        // Get the prefix to judge whethre primary table(Prefix == 0) or index table (prefix  > 0)
        int rowkeyPrefix = Bytes.toInt(rowKey.get(), 0, 4);
        byte[] rowKeyWithoutPrefix = Bytes.tail(rowKey.get(), rowKey.get().length - 4);
        Set<KeyValue> map = new TreeSet<KeyValue>(KeyValue.COMPARATOR);
        long curSize = 0;
        // stop at the end or the RAM threshold
        while (iter.hasNext() && curSize < threshold) {
            Text line = iter.next();
            String lineStr = line.toString();
            try {
                Put p = null;/*from  ww w .ja  v a2s.co  m*/
                if (rowkeyPrefix == 0) {
                    ArrayList<String> parsedLine = ParsedLine.parse(converter.getRecordSpec(), lineStr);

                    p = converter.convert(parsedLine, rowKeyWithoutPrefix);
                } else {
                    p = new Put(rowKeyWithoutPrefix);
                    if (qualifier) {
                        p.add(family, line.getBytes(), emptyByte);
                    } else {
                        p.add(family, this.qualifier, line.getBytes());
                    }
                }

                if (p != null) {
                    for (List<KeyValue> kvs : p.getFamilyMap().values()) {
                        for (KeyValue kv : kvs) {
                            map.add(kv);
                            curSize += kv.getLength();
                        }
                    }
                }
            } catch (FormatException badLine) {
                if (skipBadLines) {
                    System.err.println("Bad line." + badLine.getMessage());
                    incrementBadLineCount(1);
                    return;
                }
                throw new IOException(badLine);
            } catch (IllegalArgumentException e) {
                if (skipBadLines) {
                    System.err.println("Bad line." + e.getMessage());
                    incrementBadLineCount(1);
                    return;
                }
                throw new IOException(e);
            }
        }
        context.setStatus("Read " + map.size() + " entries of " + map.getClass() + "("
                + StringUtils.humanReadableInt(curSize) + ")");
        int index = 0;
        for (KeyValue kv : map) {
            context.write(rowKey, kv);
            if (++index > 0 && index % 100 == 0)
                context.setStatus("Wrote " + index + " key values.");
        }

        // if we have more entries to process
        if (iter.hasNext()) {
            // force flush because we cannot guarantee intra-row sorted order
            context.write(null, null);
        }
    }
}

From source file:org.apache.cassandra.cql.jdbc.HandleObjects.java

private static final <X> ByteBuffer makeByteBuffer4Set(AbstractJdbcType<?> instanceType, Set<X> value) {
    return SetMaker.getInstance(instanceType).decompose(value.getClass().cast(value));
}

From source file:org.marketcetera.util.ws.types.TypeTest.java

private static void assertSetEquals(Set<?> expected, Set<?> actual) {
    assertEquals(expected, actual);// w ww. j a  v a 2s .c  om
    /*
     * LIMITATION: the unmarshalled set is a HashSet regardless of
     * the actual type of the original set.
     */
    assertEquals(HashSet.class, actual.getClass());
}