com.googlecode.totallylazy.Pair Java Examples

The following examples show how to use com.googlecode.totallylazy.Pair. 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
Source File: StaticMemberCompleter.java    From java-repl with Apache License 2.0 6 votes vote down vote up
public CompletionResult call(String expression) throws Exception {
    final int lastSeparator = lastIndexOfSeparator(characters(" "), expression) + 1;
    final String packagePart = expression.substring(lastSeparator);

    Option<Pair<Class<?>, String>> completion = completionFor(packagePart);

    if (!completion.isEmpty()) {
        Function1<CompletionCandidate, String> value = CompletionCandidate::value;
        Sequence<CompletionCandidate> candidates = reflectionOf(completion.get().first())
                .declaredMembers()
                .filter(isStatic().and(isPublic()).and(not(isSynthetic())))
                .groupBy(candidateName())
                .map(candidate())
                .filter(where(value, startsWith(completion.get().second())));

        final int beginIndex = packagePart.lastIndexOf('.') + 1;
        return new CompletionResult(expression, lastSeparator + beginIndex, candidates);
    } else {
        return new CompletionResult(expression, 0, Sequences.empty(CompletionCandidate.class));
    }
}
 
Example #2
Source File: StaticMemberCompleter.java    From java-repl with Apache License 2.0 6 votes vote down vote up
private Option<Pair<String, Sequence<String>>> parseExpression(Pair<String, Sequence<String>> expression) {
    Option<Class<?>> expressionClass = evaluator.classFrom(expression.first());

    if (!expressionClass.isEmpty()) {
        return some(expression);
    }

    if (expression.first().contains(".")) {
        final String packagePart = expression.first().substring(0, expression.first().lastIndexOf("."));
        final String classPart = expression.first().substring(expression.first().lastIndexOf(".") + 1);

        return parseExpression(pair(packagePart, expression.second().cons(classPart)));
    }

    return Option.none();

}
 
Example #3
Source File: AbstractTreeMap.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
@Override
public int indexOf(Object pair) {
    int difference = difference(Unchecked.<Pair<K,V>>cast(pair).first());
    if (difference == 0) return left.size();
    if (difference < 0) return left.indexOf(pair);
    return 1 + left.size() + right.indexOf(pair);
}
 
Example #4
Source File: Utils.java    From java-repl with Apache License 2.0 5 votes vote down vote up
private static <T> Sequence<Sequence<T>> cartesianProductPower(Sequence<T> items, int times) {
    if (times == 0)
        return items.cartesianProduct().map(Pair.functions.values()).unsafeCast();

    return cartesianProductPower(items, times - 1)
            .cartesianProduct(items)
            .map(pair -> pair.first().append(pair.second()).unique())
            .unique();
}
 
Example #5
Source File: StaticMemberCompleter.java    From java-repl with Apache License 2.0 5 votes vote down vote up
private Option<Pair<Class<?>, String>> completionFor(String expression) {
    Option<Pair<String, Sequence<String>>> parsedClass = parseExpression(pair(expression, Sequences.empty(String.class)));

    if (!parsedClass.isEmpty() && !parsedClass.get().second().isEmpty()) {
        return some(Pair.<Class<?>, String>pair(
                evaluator.classFrom(parsedClass.get().first()).get(),
                parsedClass.get().second().toString(".").trim()));
    } else {
        return none();
    }
}
 
Example #6
Source File: TreeIterator.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
@Override
protected Pair<K, V> getNext() throws Exception {
    if(zipper.isEmpty()) return finished();
    Pair<K, V> pair = zipper.get().pair();
    zipper = zipper.get().nextOption();
    return pair;
}
 
Example #7
Source File: ConsoleWithAuthentication.java    From java-repl with Apache License 2.0 5 votes vote down vote up
@Override
public ConsoleResult execute(String expression) {
    if (!authenticated) {
        if (expression.startsWith(":login")) {
            Pair<String, Option<String>> command = parseStringCommand(expression);
            if (!command.second().isEmpty()) {
                if (command.second().get().equals(password)) {
                    authenticated = true;
                    return new ConsoleResult(expression, one(success("Logged in")));
                } else {
                    return new ConsoleResult(expression, one(error("Invalid password")));
                }
            } else {
                return new ConsoleResult(expression, one(error("Please specify password")));
            }
        } else {
            return new ConsoleResult(expression, one(error("Please authenticate first.\n    :login <password> to authenticate.\n    :logout at the end of the session to finish.")));
        }
    } else {
        if (expression.startsWith(":logout")) {
            authenticated = false;
            return new ConsoleResult(expression, one(error("Logged out")));
        } else {
            return super.execute(expression);
        }
    }
}
 
Example #8
Source File: PairParser.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
@Override
public Result<Pair<A, B>> parse(Segment<Character> characters) {
    Result<? extends A> resultA = parserA.parse(characters);
    if (resultA instanceof Failure) return cast(resultA);

    Result<? extends B> resultB = parserB.parse(resultA.remainder());
    if (resultB instanceof Failure) return cast(resultB);

    return success(Pair.pair(resultA.value(), resultB.value()), resultB.remainder());
}
 
Example #9
Source File: PersistentMapTest.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
@Test
public void canPutAndReturnOldValue() throws Exception {
    PersistentMap <Integer, String> pairs = map(4, "Four", 5, "Five", 3, "Three", 2, "Two", 6, "Six");
    Pair<PersistentMap <Integer, String>, Option<String>> result = PersistentMap.methods.put(pairs, 4, "NewFour");
    assertThat(result.first().lookup(4).get(), is("NewFour"));
    assertThat(result.second().get(), is("Four"));
}
 
Example #10
Source File: HtmlTagIndexRenderer.java    From yatspec with Apache License 2.0 5 votes vote down vote up
private Callable1<? super TestMethod, ? extends Iterable<Pair<String, TestMethod>>> methodTags() {
    return new Callable1<TestMethod, Iterable<Pair<String, TestMethod>>>() {
        @Override
        public Iterable<Pair<String, TestMethod>> call(TestMethod resultFileAndTestMethod) throws Exception {
            return sequence(tagFinder.tags(resultFileAndTestMethod)).zip(repeat(resultFileAndTestMethod));
        }
    };
}
 
Example #11
Source File: PersistentSortedMapTest.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
@Test
public void canRemoveLast() throws Exception {
    final Pair<? extends PersistentSortedMap<Integer, String>, Pair<Integer, String>> result = sortedMap(4, "Alex", 1, "Dan", 3, "Stu", 2, "Ray").removeLast();
    assertThat(result.first(), hasExactly(pair(1, "Dan"), pair(2, "Ray"), pair(3, "Stu")));
    assertThat(result.second(), is(pair(4, "Alex")));
}
 
Example #12
Source File: AbstractTreeMap.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
@Override
public Self cons(Pair<K, V> newValue) {
    return insert(newValue.first(), newValue.second());
}
 
Example #13
Source File: PersistentSortedMap.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
@Override
Pair<? extends PersistentSortedMap<K, V>, Pair<K, V>> removeFirst();
 
Example #14
Source File: PersistentSortedMap.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
@Override
Pair<? extends PersistentSortedMap<K, V>, Pair<K, V>> removeLast();
 
Example #15
Source File: PersistentSortedMap.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
@SafeVarargs
public static <K extends Comparable<? super K>, V> PersistentSortedMap<K, V> sortedMap(final Pair<K, V> head, final Pair<K, V>... tail) {
    return sortedMap(sequence(tail).cons(head));
}
 
Example #16
Source File: AbstractTreeMap.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
protected Pair<K, V> pair() {
    return Pair.pair(key, value);
}
 
Example #17
Source File: UnfoldRightIterator.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
public UnfoldRightIterator(final Function1<? super B, ? extends Option<? extends Pair<? extends A, ? extends B>>> callable, final B state) {
    this.callable = cast(callable);
    this.state = state;
}
 
Example #18
Source File: FileSource.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
public static Function1<File, Pair<String, File>> relativeTo(final File folder) {
    return file -> Pair.pair(Files.relativePath(folder, file), file);
}
 
Example #19
Source File: TreeZipper.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
public Pair<K, V> pair() {
    return Pair.pair(focus.key(), focus.value());
}
 
Example #20
Source File: Template.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
Appendable appendPairs(Anonymous anonymous, Iterable<? extends Pair<?, ?>> pairs, Appendable appendable) throws Exception {
    return sequence(pairs).fold(appendable, (a, p) ->
            append(anonymous,
                    map(sequence(anonymous.paramaeterNames()).zip(sequence(p.second(), p.first()))), a));
}
 
Example #21
Source File: PersistentSortedMapTest.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
@Test
public void canRemoveFirst() throws Exception {
    final Pair<? extends PersistentSortedMap<Integer, String>, Pair<Integer, String>> result = sortedMap(4, "Alex", 1, "Dan", 3, "Stu", 2, "Ray").removeFirst();
    assertThat(result.first(), hasExactly(pair(2, "Ray"), pair(3, "Stu"), pair(4, "Alex")));
    assertThat(result.second(), is(pair(1, "Dan")));
}
 
Example #22
Source File: ListMap.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
@Override
public <C extends Segment<Pair<K, V>>> C joinTo(C rest) {
    return list.joinTo(rest);
}
 
Example #23
Source File: AtomicMap.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
private Pair<PersistentMap<K, V>, V> put(PersistentMap<K, V> map, K key, V value) {
    return PersistentMap.methods.put(map, key, value).
            second(Option.functions.<V>getOrNull());
}
 
Example #24
Source File: AbstractMapFactory.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
@Override
public M map(Iterable<? extends Pair<K, V>> values) {
    return sequence(values).fold(this.empty(), Segment.functions.<Pair<K, V>, M>cons());
}
 
Example #25
Source File: Function2.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
default Function1<Pair<A, B>, C> pair() {
     return Callables.pair(this);
}
 
Example #26
Source File: TreeMapFactory.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
@Override
public T map(Iterable<? extends Pair<K, V>> values) {
    return cast(TreeMap.methods.treeMap(factory, comparator, sequence(values).toSortedList(Comparators.<Pair<K, V>, K>by(Callables.<K>first(), comparator))));
}
 
Example #27
Source File: PersistentMap.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
public static <K, V, M extends PersistentMap<K, V>> Pair<M, Option<V>> remove(M map, K key) {
    return Pair.pair(Unchecked.<M>cast(map.delete(key)), map.lookup(key));
}
 
Example #28
Source File: PersistentMap.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
public static <K, V, M extends PersistentMap<K, V>> Pair<M, Option<V>> put(M map, K key, V newValue) {
    return Pair.pair(Unchecked.<M>cast(map.insert(key, newValue)), map.lookup(key));
}
 
Example #29
Source File: Parser.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
default <B> Parser<Pair<A, B>> then(Parser<? extends B> parser) {
    return PairParser.pair(this, parser);
}
 
Example #30
Source File: TreeSet.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
@Override
public Pair<PersistentSortedSet<T>, T> removeFirst() {
    return treeSet(map.removeFirst());
}