File: SimpleFunctionList.java

package info (click to toggle)
libglazedlists-java 1.9.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,012 kB
  • sloc: java: 22,561; xml: 940; makefile: 5
file content (57 lines) | stat: -rw-r--r-- 1,678 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package ca.odell.glazedlists.impl;

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.TransformedList;
import ca.odell.glazedlists.FunctionList.Function;
import ca.odell.glazedlists.event.ListEvent;

/**
 * A {@link TransformedList} that maps each element of a source list to a target element by use
 * of a specified {@link Function}.
 * <p>
 * <strong><font color="#FF0000">Warning:</font></strong> This class is thread ready but not
 * thread safe. See {@link EventList} for an example of thread safe code.
 *
 * @see TransformedList
 * @author Holger Brands
 */
public final class SimpleFunctionList<S, E> extends TransformedList<S, E> {

    /** function to transform each list element. */
    private final Function<S,E> function;

    /**
     * Constructs a SimpleFunctionList that maps the elements of the given source list by the
     * specified function.
     *
     * @param source the source list to transform
     * @param function the mapping function
     */
    public SimpleFunctionList(EventList<S> source, Function<S, E> function) {
        super(source);
        Preconditions.checkNotNull(function, "mapping function is undefined");
        this.function = function;
        source.addListEventListener(this);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public E get(int index) {
        final S elem = source.get(index);
        return function.evaluate(elem);
    }

    /** {@inheritDoc} */
    @Override
    public void listChanged(ListEvent<S> listChanges) {
        updates.forwardEvent(listChanges);
    }

    /** {@inheritDoc} */
    @Override
    protected boolean isWritable() {
        return false;
    }
}