public static <E> PVector<E> vector() { return TreePVector.empty(); }
@Override public TreePVector<E> subList(final int start, final int end) { final int size = size(); if(start<0 || end>size || start>end) throw new IndexOutOfBoundsException(); if(start==end) return empty(); if(start==0) { if(end==size) return this; // remove from end: return this.minus(size-1).subList(start, end); } // remove from start: return this.minus(0).subList(start-1,end-1); }
/** * @param <E> * @param list * @return empty().plusAll(list) */ @SuppressWarnings("unchecked") public static <E> TreePVector<E> from(final Collection<? extends E> list) { if(list instanceof TreePVector) return (TreePVector<E>)list; //(actually we only know it's TreePVector<? extends E>) // but that's good enough for an immutable // (i.e. we can't mess someone else up by adding the wrong type to it) return TreePVector.<E>empty().plusAll(list); }
PVector<Integer> pvec = TreePVector.empty(); List<Integer> list = new LinkedList<Integer>(); Random r = new Random(); UtilityTest.assertEqualsAndHash(list, pvec); UtilityTest.assertEqualsAndHash(pvec, TreePVector.from(pvec)); UtilityTest.assertEqualsAndHash(TreePVector.empty(), pvec.minusAll(pvec)); UtilityTest.assertEqualsAndHash(pvec, TreePVector.empty().plusAll(pvec)); UtilityTest.assertEqualsAndHash(pvec, TreePVector.singleton(10).plusAll(1,pvec).minus(0));
public PVector<E> with(final int i, final E e) { if(i<0 || i>=size()) throw new IndexOutOfBoundsException(); IntTreePMap<E> map = this.map.plus(i, e); if(map==this.map) return this; return new TreePVector<E>( map ); } }
@Override public PVector<V> applyUnsafe(JsonParser p) throws IOException { List<V> values = new LinkedList<>(); /* * If we are in the root context (not inside an object or list) then we need to consume the next token * before attempting to call child deserialisers. */ if (p.getParsingContext().inRoot()) { if (p.nextToken() == JsonToken.END_ARRAY) { return TreePVector.empty(); } } if (JsonToken.VALUE_NULL != p.getCurrentToken()) { /* * When the parser has hit the end of input nextToken() will always return null. * So need to prevent infinite loops we check the parser closed flag. */ while (!p.isClosed() && (p.nextToken() != JsonToken.END_ARRAY)) { values.add(itemDeserialiser.apply(p)); } } return TreePVector.from(values); } }
public ActivateFlowsAction(String definitionId, Collection<Flow> flows) { this(definitionId, TreePVector.from(flows)); }
public void testSerializationForTreePVector() { final TreePVector<String> initialTreePVector = TreePVector.from(Arrays.asList(ELEMENT1, ELEMENT2, ELEMENT3)); final byte[] serializedTreePVector = serializeToByteArray(initialTreePVector); assertNotNull(serializedTreePVector); assertTrue(serializedTreePVector.length > 0); @SuppressWarnings("unchecked") final TreePVector<String> deserializedTreePVector = deserializeFromByteArray(serializedTreePVector, TreePVector.class); assertNotNull(deserializedTreePVector); assertEquals(3, deserializedTreePVector.size()); assertNotSame(initialTreePVector, deserializedTreePVector); assertEquals(0, deserializedTreePVector.indexOf(ELEMENT1)); assertEquals(1, deserializedTreePVector.indexOf(ELEMENT2)); assertEquals(2, deserializedTreePVector.indexOf(ELEMENT3)); }
Always(final Function<Object, ? extends T> s) { super(TreePVector.singleton(Rec.raw(s))); }
public TreePVector<E> minusAll(final Collection<?> list) { TreePVector<E> result = this; for(Object e : list) result = result.minus(e); return result; }
@Override public E get(final int index) { if(index<0 || index>=size()) throw new IndexOutOfBoundsException(); return map.get(index); }
public PVector<E> with(final int i, final E e) { if(i<0 || i>=size()) throw new IndexOutOfBoundsException(); IntTreePMap<E> map = this.map.plus(i, e); if(map==this.map) return this; return new TreePVector<E>( map ); } }
/** * <pre> * {@code * PVector<Integer> q = Reducers.<Integer>toPVector() .mapReduce(Stream.of(1,2,3,4)); * * } * </pre> * @return Reducer for PVector */ public static <T> Reducer<PVector<T>> toPVector() { return Reducer.<PVector<T>> of(TreePVector.empty(), (final PVector<T> a) -> b -> a.plusAll(b), (final T x) -> TreePVector.singleton(x)); } /**
/** * Create a PVector from the supplied Colleciton * <pre> * {@code * PVector<Integer> list = PVectors.fromCollection(Arrays.asList(1,2,3)); * * } * </pre> * * @param values to add to new PVector * @return PVector containing values */ public static <T> PVector<T> fromCollection(Collection<T> values){ return TreePVector.from(values); } /**
/** * Construct a PVector containing a single value * </pre> * {@code * List<String> single = PVectors.singleton("1"); * * //or * * PVector<String> single = PVectors.singleton("1"); * * } * </pre> * * @param value Single value for PVector * @return PVector with a single value */ public static <T> PVector<T> singleton(T value){ return TreePVector.singleton(value); } /**
public TreePVector<E> minusAll(final Collection<?> list) { TreePVector<E> result = this; for(Object e : list) result = result.minus(e); return result; }
@Override public E get(final int index) { if(index<0 || index>=size()) throw new IndexOutOfBoundsException(); return map.get(index); }