/** An empty explanation. */ public static Violation absent() { return create(ConsPStack.empty()); } }
/** Creates an explanation with one step. */ public static Violation of(String reason) { return create(ConsPStack.singleton(reason)); }
@Override public ConsPStack<E> subList(final int start, final int end) { if(start<0 || end>size || start>end) throw new IndexOutOfBoundsException(); if(end==size) // want a substack return subList(start); // this is faster if(start==end) // want nothing return empty(); if(start==0) // want the current element return new ConsPStack<E>(first, rest.subList(0, end-1)); // otherwise, don't want the current element: return rest.subList(start-1, end-1); }
public ConsPStack<E> plusAll(final int i, final Collection<? extends E> list) { // TODO inefficient if list.isEmpty() if(i<0 || i>size) throw new IndexOutOfBoundsException(); if(i==0) return plusAll(list); return new ConsPStack<E>(first, rest.plusAll(i-1, list)); }
/** * Construct a Cases instance from a list of Pattern Matching Cases * Will execute sequentially when Match is called. * * @param cases Persistent Stack of cases to build Cases from * @return New Cases instance (sequential) */ public static <T,R,X extends Function<T,R>> Cases<T,R,X> ofList(List<Case<T,R,X>> cases){ return new Cases(cases.stream().map(ConsPStack::singleton) .reduce(ConsPStack.empty(),(acc,next)-> acc.plus(acc.size(),next.get(0))),true); }
PStack<Integer> pstack = ConsPStack.empty(); List<Integer> list = new LinkedList<Integer>(); Random r = new Random(); UtilityTest.assertEqualsAndHash(list, pstack); UtilityTest.assertEqualsAndHash(pstack, ConsPStack.from(pstack)); UtilityTest.assertEqualsAndHash(ConsPStack.empty(), pstack.minusAll(pstack)); UtilityTest.assertEqualsAndHash(pstack, ConsPStack.empty().plusAll(UtilityTest.reverse(pstack))); UtilityTest.assertEqualsAndHash(pstack, ConsPStack.singleton(10).plusAll(1,UtilityTest.reverse(pstack)).minus(0));
public void testStack() { List<String> list = Arrays.asList("one","two","","three"); PStack<String> stack = ConsPStack.empty(); PStack<String> stack2 = ConsPStack.from(list); assertEqualsAndHash(stack3.minusAll(stack3), ConsPStack.empty()); assertEqualsAndHash(stack3.minusAll(stack), ConsPStack.empty());
/** * @param <E> * @param list * @return a stack consisting of the elements of list in the order of list.iterator() */ @SuppressWarnings("unchecked") public static <E> ConsPStack<E> from(final Collection<? extends E> list) { if(list instanceof ConsPStack) return (ConsPStack<E>)list; //(actually we only know it's ConsPStack<? 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 ConsPStack.<E>from(list.iterator()); }
public void testSerializationForConsPStack() { final ConsPStack<String> initialConsPStack = ConsPStack.from(Arrays.asList(ELEMENT1, ELEMENT2, ELEMENT3)); final byte[] serializedConsPStack = serializeToByteArray(initialConsPStack); assertNotNull(serializedConsPStack); assertTrue(serializedConsPStack.length > 0); @SuppressWarnings("unchecked") final ConsPStack<String> deserializedConsPStack = deserializeFromByteArray(serializedConsPStack, ConsPStack.class); assertNotNull(deserializedConsPStack); assertEquals(3, deserializedConsPStack.size()); assertNotSame(initialConsPStack, deserializedConsPStack); assertEquals(ELEMENT1, deserializedConsPStack.get(0)); assertEquals(ELEMENT2, deserializedConsPStack.get(1)); assertEquals(ELEMENT3, deserializedConsPStack.get(2)); }
public ConsPStack<E> minus(final int i) { if (i < 0 || i >= size) throw new IndexOutOfBoundsException("Index: " + i + "; size: " + size); else if (i == 0) return rest; else return new ConsPStack<E>(first, rest.minus(i-1)); }
public ConsPStack<E> plus(final int i, final E e) { if(i<0 || i>size) throw new IndexOutOfBoundsException(); if(i==0) // insert at beginning return plus(e); return new ConsPStack<E>(first, rest.plus(i-1, e)); }
/** Adds a step. */ public Violation plus(String edge) { return create(path().plus(edge)); }
/** * Construct a Cases instance from an array Pattern Matching Cases * Will execute sequentially when Match is called. * * @param cazes Array of cases to build Cases instance from * @return New Cases instance (sequential) */ public static <T,R,X extends Function<T,R>> Cases<T,R,X> of(Case<T,R,X>... cazes){ return ofPStack( Stream.of(cazes) .map(ConsPStack::singleton) .reduce(ConsPStack.empty(),(acc,next)-> acc.plus(acc.size(),next.get(0)))); } /**
@Override public ConsPStack<E> subList(final int start, final int end) { if(start<0 || end>size || start>end) throw new IndexOutOfBoundsException(); if(end==size) // want a substack return subList(start); // this is faster if(start==end) // want nothing return empty(); if(start==0) // want the current element return new ConsPStack<E>(first, rest.subList(0, end-1)); // otherwise, don't want the current element: return rest.subList(start-1, end-1); }