Example usage for org.apache.commons.collections4 Transformer Transformer

List of usage examples for org.apache.commons.collections4 Transformer Transformer

Introduction

In this page you can find the example usage for org.apache.commons.collections4 Transformer Transformer.

Prototype

Transformer

Source Link

Usage

From source file:org.settings4j.util.ELConnectorWrapper.java

/**
 * Usage: <code>${connectors.object['xyz']}</code> returns the first founded Value in all Connectors:
 * <code>connector.getObject("xyz");</code>.
 *
 * @return the first founded Value in all connectors
 *//* www  .j av a 2s.  c  o m*/
public Map<String, Object> getObject() {
    final Transformer<String, Object> transformer = new Transformer<String, Object>() {

        @Override
        public Object transform(final String key) {
            if (key != null) {
                for (Connector connector : ELConnectorWrapper.this.connectors) {
                    final Object result = connector.getObject(key);
                    if (result != null) {
                        return result;
                    }
                }
            }
            return null;
        }
    };
    return LazyMap.lazyMap(new HashMap<String, Object>(), transformer);
}

From source file:uniol.apt.analysis.synthesize.FindWords.java

static private void generateList(final PNProperties properties, SortedSet<Character> alphabet,
        final boolean quickFail, WordCallback wordCallback, LengthDoneCallback lengthDoneCallback,
        ForkJoinPool executor) throws PreconditionFailedException {
    if (properties.isPlain() && properties.isKMarking())
        throw new PreconditionFailedException("The combination of plain and k-marking is not supported"
                + ", because 'minimal unsolvable' cannot be defined");

    CompletionService<Pair<String, SynthesizePN>> completion = new ExecutorCompletionService<>(executor);
    List<String> currentLevel = Collections.singletonList("");
    while (!currentLevel.isEmpty()) {

        // Lazily create new Callables to avoid OOM errors
        Iterator<Callable<Pair<String, SynthesizePN>>> jobGenerator = IteratorUtils.transformedIterator(
                new NextWordsIterator(properties, alphabet, currentLevel),
                new Transformer<String, Callable<Pair<String, SynthesizePN>>>() {
                    @Override//from  w  w  w .jav  a2 s  .c om
                    public Callable<Pair<String, SynthesizePN>> transform(final String word) {
                        return new Callable<Pair<String, SynthesizePN>>() {
                            @Override
                            public Pair<String, SynthesizePN> call() {
                                List<Character> wordList = toList(word);
                                SynthesizePN synthesize = solveWord(wordList, properties, quickFail);
                                return new Pair<>(word, synthesize);
                            }
                        };
                    }
                });

        // Wait for and handle results
        List<String> nextLevel = new ArrayList<>();
        int tasksSubmitted = submitTasks(executor, completion, jobGenerator);
        int tasksFinished = 0;
        while (tasksSubmitted != tasksFinished) {
            String word;
            SynthesizePN synthesize;
            try {
                Pair<String, SynthesizePN> pair = completion.take().get();
                word = pair.getFirst();
                synthesize = pair.getSecond();
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                return;
            }

            List<Character> wordList = toList(word);
            wordCallback.call(wordList, word, synthesize);
            if (synthesize.wasSuccessfullySeparated()) {
                nextLevel.add(word);
            }
            tasksFinished++;

            tasksSubmitted += submitTasks(executor, completion, jobGenerator);
        }

        int currentLength = currentLevel.iterator().next().length() + 1;
        lengthDoneCallback.call(currentLength);
        currentLevel = nextLevel;
        Collections.sort(currentLevel);
    }
}