Search in sources :

Example 6 with AbstractSequentialList

use of java.util.AbstractSequentialList in project robovm by robovm.

the class AbstractSequentialListTest method test_removeI.

public void test_removeI() {
    AbstractSequentialList asl = new AbstractSequentialList() {

        String[] buff = { "0", "1", "2", "3", "4", "5" };

        int currPos = 0;

        @Override
        public int size() {
            return buff.length;
        }

        @Override
        public ListIterator listIterator(int index) {
            currPos = index;
            return new ListIterator() {

                public void add(Object o) {
                }

                public boolean hasNext() {
                    return true;
                }

                public boolean hasPrevious() {
                    return false;
                }

                public Object next() {
                    return buff[currPos];
                }

                public int nextIndex() {
                    return 0;
                }

                public Object previous() {
                    return null;
                }

                public int previousIndex() {
                    return 0;
                }

                public void remove() {
                    buff[currPos] = "removed element";
                }

                public void set(Object o) {
                }
            };
        }
    };
    try {
        asl.remove(asl.size() + 1);
        fail("IndexOutOfBoundsException expected");
    } catch (IndexOutOfBoundsException e) {
    //expected
    }
    try {
        asl.remove(-1);
        fail("IndexOutOfBoundsException expected");
    } catch (IndexOutOfBoundsException e) {
    //expected
    }
    for (int i = 0; i < asl.size(); i++) {
        assertFalse(asl.get(i).toString().contains("removed element"));
        asl.remove(i);
        assertTrue(asl.get(i).toString().contains("removed element"));
    }
}
Also used : ListIterator(java.util.ListIterator) AbstractSequentialList(java.util.AbstractSequentialList)

Example 7 with AbstractSequentialList

use of java.util.AbstractSequentialList in project robovm by robovm.

the class AbstractSequentialListTest method test_setILjava_lang_Object.

public void test_setILjava_lang_Object() {
    AbstractSequentialList asl = new AbstractSequentialList() {

        String[] buff = { "0", "1", "2", "3", "4", "5" };

        final String illegalStr = "Illegal element";

        int currPos = 0;

        @Override
        public int size() {
            return buff.length;
        }

        @Override
        public ListIterator listIterator(int index) {
            currPos = index;
            return new ListIterator() {

                public void add(Object o) {
                }

                public boolean hasNext() {
                    return true;
                }

                public boolean hasPrevious() {
                    return false;
                }

                public Object next() {
                    return buff[currPos];
                }

                public int nextIndex() {
                    return 0;
                }

                public Object previous() {
                    return null;
                }

                public int previousIndex() {
                    return 0;
                }

                public void remove() {
                    buff[currPos] = "removed element";
                }

                public void set(Object o) {
                    if (o == null)
                        throw new NullPointerException();
                    if (o.equals(illegalStr))
                        throw new IllegalArgumentException();
                    buff[currPos] = (String) o;
                }
            };
        }
    };
    try {
        asl.set(asl.size() + 1, "new element");
        fail("IndexOutOfBoundsException expected");
    } catch (IndexOutOfBoundsException e) {
    //expected
    }
    try {
        asl.set(-1, "new element");
        fail("IndexOutOfBoundsException expected");
    } catch (IndexOutOfBoundsException e) {
    //expected
    }
    for (int i = 0; i < asl.size(); i++) {
        assertFalse(asl.get(i).toString().contains("new element"));
        asl.set(i, "new element");
        assertTrue(asl.get(i).toString().contains("new element"));
    }
    try {
        asl.set(1, new Double(1));
        fail("ClassCastException expected");
    } catch (ClassCastException e) {
    //
    }
    try {
        asl.set(1, "Illegal element");
        fail("IllegalArgumentException expected");
    } catch (IllegalArgumentException ee) {
    //expected
    }
    try {
        asl.set(1, null);
        fail("NullPointerException expected");
    } catch (NullPointerException ee) {
    //expected
    }
    asl = new AbstractSequentialList() {

        @Override
        public int size() {
            return 0;
        }

        @Override
        public ListIterator listIterator(int index) {
            return new Mock_unsupportedListIterator();
        }
    };
    try {
        asl.set(0, "New element");
        fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException e) {
    //expected
    }
}
Also used : ListIterator(java.util.ListIterator) AbstractSequentialList(java.util.AbstractSequentialList)

Example 8 with AbstractSequentialList

use of java.util.AbstractSequentialList in project robovm by robovm.

the class AbstractSequentialListTest method test_getI.

public void test_getI() {
    final String[] buff = { "0", "1", "2", "3", "4", "5" };
    AbstractSequentialList asl = new AbstractSequentialList() {

        int currPos = 0;

        @Override
        public int size() {
            return buff.length;
        }

        @Override
        public ListIterator listIterator(int index) {
            currPos = index;
            return new ListIterator() {

                public void add(Object o) {
                }

                public boolean hasNext() {
                    return true;
                }

                public boolean hasPrevious() {
                    return false;
                }

                public Object next() {
                    return buff[currPos];
                }

                public int nextIndex() {
                    return 0;
                }

                public Object previous() {
                    return null;
                }

                public int previousIndex() {
                    return 0;
                }

                public void remove() {
                }

                public void set(Object o) {
                }
            };
        }
    };
    for (int i = 0; i < buff.length; i++) {
        assertEquals(buff[i], asl.get(i));
    }
    try {
        asl.get(asl.size() + 1);
        fail("IndexOutOfBoundsException expected");
    } catch (IndexOutOfBoundsException e) {
    //expected
    }
    try {
        asl.get(-1);
        fail("IndexOutOfBoundsException expected");
    } catch (IndexOutOfBoundsException e) {
    //expected
    }
}
Also used : ListIterator(java.util.ListIterator) AbstractSequentialList(java.util.AbstractSequentialList)

Example 9 with AbstractSequentialList

use of java.util.AbstractSequentialList in project hive by apache.

the class PartitionPruner method prunePartitionNames.

/**
 * Prunes partition names to see if they match the prune expression.
 * @param partColumnNames name of partition columns
 * @param partColumnTypeInfos types of partition columns
 * @param prunerExpr The expression to match.
 * @param defaultPartitionName name of default partition
 * @param partNames Partition names to filter. The list is modified in place.
 * @return Whether the list has any partitions for which the expression may or may not match.
 */
public static boolean prunePartitionNames(List<String> partColumnNames, List<PrimitiveTypeInfo> partColumnTypeInfos, ExprNodeGenericFuncDesc prunerExpr, String defaultPartitionName, List<String> partNames) throws HiveException, MetaException {
    // Prepare the expression to filter on the columns.
    ObjectPair<PrimitiveObjectInspector, ExprNodeEvaluator> handle = PartExprEvalUtils.prepareExpr(prunerExpr, partColumnNames, partColumnTypeInfos);
    // Filter the name list. Removing elements one by one can be slow on e.g. ArrayList,
    // so let's create a new list and copy it if we don't have a linked list
    boolean inPlace = partNames instanceof AbstractSequentialList<?>;
    List<String> partNamesSeq = inPlace ? partNames : new LinkedList<String>(partNames);
    // Array for the values to pass to evaluator.
    ArrayList<String> values = new ArrayList<String>(partColumnNames.size());
    for (int i = 0; i < partColumnNames.size(); ++i) {
        values.add(null);
    }
    boolean hasUnknownPartitions = false;
    Iterator<String> partIter = partNamesSeq.iterator();
    while (partIter.hasNext()) {
        String partName = partIter.next();
        Warehouse.makeValsFromName(partName, values);
        ArrayList<Object> convertedValues = new ArrayList<Object>(values.size());
        for (int i = 0; i < values.size(); i++) {
            String partitionValue = values.get(i);
            PrimitiveTypeInfo typeInfo = partColumnTypeInfos.get(i);
            if (partitionValue.equals(defaultPartitionName)) {
                // Null for default partition.
                convertedValues.add(null);
            } else {
                Object o = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(typeInfo)).convert(partitionValue);
                convertedValues.add(o);
            }
        }
        // Evaluate the expression tree.
        Boolean isNeeded = (Boolean) PartExprEvalUtils.evaluateExprOnPart(handle, convertedValues);
        boolean isUnknown = (isNeeded == null);
        if (!isUnknown && !isNeeded) {
            partIter.remove();
            continue;
        }
        if (isUnknown && values.contains(defaultPartitionName)) {
            // Note that predicate would only contains partition column parts of original predicate.
            if (LOG.isDebugEnabled()) {
                LOG.debug("skipping default/bad partition: " + partName);
            }
            partIter.remove();
            continue;
        }
        hasUnknownPartitions |= isUnknown;
        if (LOG.isDebugEnabled()) {
            LOG.debug("retained " + (isUnknown ? "unknown " : "") + "partition: " + partName);
        }
    }
    if (!inPlace) {
        partNames.clear();
        partNames.addAll(partNamesSeq);
    }
    return hasUnknownPartitions;
}
Also used : ExprNodeEvaluator(org.apache.hadoop.hive.ql.exec.ExprNodeEvaluator) ArrayList(java.util.ArrayList) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) AbstractSequentialList(java.util.AbstractSequentialList) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)

Aggregations

AbstractSequentialList (java.util.AbstractSequentialList)9 ListIterator (java.util.ListIterator)6 LinkedList (java.util.LinkedList)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Vector (java.util.Vector)1 ExprNodeEvaluator (org.apache.hadoop.hive.ql.exec.ExprNodeEvaluator)1 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)1 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)1