|
| 1 | +package org.codefx.libfx.collection.transform; |
| 2 | + |
| 3 | +import java.util.Collection; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Objects; |
| 6 | +import java.util.Set; |
| 7 | +import java.util.function.Function; |
| 8 | + |
| 9 | +/** |
| 10 | + * Builder for {@link TransformingCollection}s, {@link TransformingSet}s and {@link TransformingList}s. |
| 11 | + * <p> |
| 12 | + * A builder can be obtained by calling {@link #forInnerAndOuterType(Class, Class) forInnerAndOuterType} or |
| 13 | + * {@link #forInnerAndOuterTypeUnknown()}. The building methods {@code transform...} can only be called after |
| 14 | + * transformations from inner to outer elements and vice versa have been set. |
| 15 | + * |
| 16 | + * @param <I> |
| 17 | + * the inner type of the created transforming collection, i.e. the type of the elements contained in the |
| 18 | + * wrapped/inner collection |
| 19 | + * @param <O> |
| 20 | + * the outer type of the created transforming collection, i.e. the type of elements appearing to be in the |
| 21 | + * created collection |
| 22 | + */ |
| 23 | +public class TransformingCollectionBuilder<I, O> { |
| 24 | + |
| 25 | + // #begin FIELDS |
| 26 | + |
| 27 | + private final Class<? super O> outerTypeToken; |
| 28 | + private final Class<? super I> innerTypeToken; |
| 29 | + |
| 30 | + private Function<? super I, ? extends O> transformToOuter; |
| 31 | + private Function<? super O, ? extends I> transformToInner; |
| 32 | + |
| 33 | + // #end FIELDS |
| 34 | + |
| 35 | + // #begin CONSTRUCTION |
| 36 | + |
| 37 | + private TransformingCollectionBuilder(Class<? super I> innerTypeToken, Class<? super O> outerTypeToken) { |
| 38 | + Objects.requireNonNull(innerTypeToken, "The argument 'innerTypeToken' must not be null."); |
| 39 | + Objects.requireNonNull(outerTypeToken, "The argument 'outerTypeToken' must not be null."); |
| 40 | + |
| 41 | + this.outerTypeToken = outerTypeToken; |
| 42 | + this.innerTypeToken = innerTypeToken; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Creates a new builder for the specified inner and outer type. |
| 47 | + * |
| 48 | + * @param <I> |
| 49 | + * the inner type of the created transforming collection, i.e. the type of the elements contained in the |
| 50 | + * wrapped/inner collection |
| 51 | + * @param <O> |
| 52 | + * the outer type of the created transforming collection, i.e. the type of elements appearing to be in |
| 53 | + * the created collection |
| 54 | + * @param innerTypeToken |
| 55 | + * the token for the inner type |
| 56 | + * @param outerTypeToken |
| 57 | + * the token for the outer type |
| 58 | + * @return a new builder |
| 59 | + */ |
| 60 | + public static <I, O> TransformingCollectionBuilder<I, O> forInnerAndOuterType( |
| 61 | + Class<? super I> innerTypeToken, Class<? super O> outerTypeToken) { |
| 62 | + return new TransformingCollectionBuilder<>(innerTypeToken, outerTypeToken); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates a new builder for unknown inner and outer types. |
| 67 | + * <p> |
| 68 | + * This is equivalent to calling {@link #forInnerAndOuterType(Class, Class) forInnerAndOuterType(Object.class, |
| 69 | + * Object.class)}. To obtain a builder for {@code <I, O>} you will have to call |
| 70 | + * {@code TransformingCollectionBuilder.<I, O> forInnerAndOuterTypeUnknown()}. |
| 71 | + * |
| 72 | + * @param <I> |
| 73 | + * the inner type of the created transforming collection, i.e. the type of the elements contained in the |
| 74 | + * wrapped/inner collection |
| 75 | + * @param <O> |
| 76 | + * the outer type of the created transforming collection, i.e. the type of elements appearing to be in |
| 77 | + * the created collection |
| 78 | + * @return a new builder |
| 79 | + */ |
| 80 | + public static <I, O> TransformingCollectionBuilder<I, O> forInnerAndOuterTypeUnknown() { |
| 81 | + return forInnerAndOuterType(Object.class, Object.class); |
| 82 | + } |
| 83 | + |
| 84 | + // #end CONSTRUCTION |
| 85 | + |
| 86 | + // #begin SET FIELDS |
| 87 | + |
| 88 | + /** |
| 89 | + * Sets the transformation from inner to outer elements which will be used by the created collection. |
| 90 | + * |
| 91 | + * @param transformToOuter |
| 92 | + * transforms inner to outer elements |
| 93 | + * @return this builder |
| 94 | + */ |
| 95 | + public TransformingCollectionBuilder<I, O> toOuter(Function<? super I, ? extends O> transformToOuter) { |
| 96 | + Objects.requireNonNull(transformToOuter, "The argument 'transformToOuter' must not be null."); |
| 97 | + |
| 98 | + this.transformToOuter = transformToOuter; |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Sets the transformation from outer to inner elements which will be used by the created collection. |
| 104 | + * |
| 105 | + * @param transformToInner |
| 106 | + * transforms outer to inner elements |
| 107 | + * @return this builder |
| 108 | + */ |
| 109 | + public TransformingCollectionBuilder<I, O> toInner(Function<? super O, ? extends I> transformToInner) { |
| 110 | + Objects.requireNonNull(transformToInner, "The argument 'transformToInner' must not be null."); |
| 111 | + |
| 112 | + this.transformToInner = transformToInner; |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + // #end SET FIELDS |
| 117 | + |
| 118 | + // #begin BUILD |
| 119 | + |
| 120 | + /** |
| 121 | + * Creates a {@link TransformingCollection} which transforms/decorates the specified collection. |
| 122 | + * |
| 123 | + * @param collection |
| 124 | + * the collection to transform; will be the inner collection of the returned transformation |
| 125 | + * @return a new {@link TransformingCollection} |
| 126 | + */ |
| 127 | + public TransformingCollection<I, O> transformCollection(Collection<I> collection) { |
| 128 | + return new TransformingCollection<>( |
| 129 | + collection, innerTypeToken, outerTypeToken, transformToOuter, transformToInner); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Creates a {@link TransformingSet} which transforms/decorates the specified set. |
| 134 | + * |
| 135 | + * @param set |
| 136 | + * the set to transform; will be the inner set of the returned transformation |
| 137 | + * @return a new {@link TransformingSet} |
| 138 | + */ |
| 139 | + public TransformingSet<I, O> transformSet(Set<I> set) { |
| 140 | + return new TransformingSet<>( |
| 141 | + set, innerTypeToken, outerTypeToken, transformToOuter, transformToInner); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Creates a {@link TransformingList} which transforms/decorates the specified list. |
| 146 | + * |
| 147 | + * @param list |
| 148 | + * the list to transform; will be the inner list of the returned transformation |
| 149 | + * @return a new {@link TransformingList} |
| 150 | + */ |
| 151 | + public TransformingList<I, O> transformList(List<I> list) { |
| 152 | + return new TransformingList<>( |
| 153 | + list, innerTypeToken, outerTypeToken, transformToOuter, transformToInner); |
| 154 | + } |
| 155 | + |
| 156 | + // #end BUILD |
| 157 | + |
| 158 | +} |
0 commit comments