|
| 1 | +package org.codefx.libfx.collection.transform; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | +import java.util.Objects; |
| 5 | +import java.util.function.Function; |
| 6 | + |
| 7 | +/** |
| 8 | + * Builder for {@link TransformingMap}s. |
| 9 | + * <p> |
| 10 | + * A builder can be obtained by calling {@link #forTypes(Class, Class, Class, Class) forTypes} or |
| 11 | + * {@link #forTypesUnknown()}. The building method TODO can only be called after transformations from inner to outer |
| 12 | + * keys and values and vice versa have been set. |
| 13 | + * |
| 14 | + * @param <IK> |
| 15 | + * the inner key type of the created transforming map, i.e. the type of the keys contained in the |
| 16 | + * wrapped/inner map |
| 17 | + * @param <OK> |
| 18 | + * the outer key type of the created transforming map, i.e. the type of keys appearing to be in this map |
| 19 | + * @param <IV> |
| 20 | + * the inner value type of the created transforming map, i.e. the type of the values contained in the |
| 21 | + * wrapped/inner map |
| 22 | + * @param <OV> |
| 23 | + * the outer value type of the created transforming map, i.e. the type of values appearing to be in this map |
| 24 | + */ |
| 25 | +public class TransformingMapBuilder<IK, OK, IV, OV> { |
| 26 | + |
| 27 | + // #begin FIELDS |
| 28 | + |
| 29 | + private final Class<? super OK> outerKeyTypeToken; |
| 30 | + private final Class<? super IK> innerKeyTypeToken; |
| 31 | + private Function<? super IK, ? extends OK> transformToOuterKey; |
| 32 | + private Function<? super OK, ? extends IK> transformToInnerKey; |
| 33 | + |
| 34 | + private final Class<? super OV> outerValueTypeToken; |
| 35 | + private final Class<? super IV> innerValueTypeToken; |
| 36 | + private Function<? super IV, ? extends OV> transformToOuterValue; |
| 37 | + private Function<? super OV, ? extends IV> transformToInnerValue; |
| 38 | + |
| 39 | + // #end FIELDS |
| 40 | + |
| 41 | + // #begin CONSTRUCTION |
| 42 | + |
| 43 | + private TransformingMapBuilder( |
| 44 | + Class<? super IK> innerKeyTypeToken, Class<? super OK> outerKeyTypeToken, |
| 45 | + Class<? super IV> innerValueTypeToken, Class<? super OV> outerValueTypeToken) { |
| 46 | + |
| 47 | + Objects.requireNonNull(innerKeyTypeToken, "The argument 'innerKeyTypeToken' must not be null."); |
| 48 | + Objects.requireNonNull(outerKeyTypeToken, "The argument 'outerKeyTypeToken' must not be null."); |
| 49 | + Objects.requireNonNull(innerValueTypeToken, "The argument 'innerValueTypeToken' must not be null."); |
| 50 | + Objects.requireNonNull(outerValueTypeToken, "The argument 'outerValueTypeToken' must not be null."); |
| 51 | + |
| 52 | + this.innerKeyTypeToken = innerKeyTypeToken; |
| 53 | + this.outerKeyTypeToken = outerKeyTypeToken; |
| 54 | + this.innerValueTypeToken = innerValueTypeToken; |
| 55 | + this.outerValueTypeToken = outerValueTypeToken; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates a new builder for the specified inner and outer key and value types. |
| 60 | + * |
| 61 | + * @param <IK> |
| 62 | + * the inner key type of the created transforming map, i.e. the type of the keys contained in the |
| 63 | + * wrapped/inner map |
| 64 | + * @param <OK> |
| 65 | + * the outer key type of the created transforming map, i.e. the type of keys appearing to be in this map |
| 66 | + * @param <IV> |
| 67 | + * the inner value type of the created transforming map, i.e. the type of the values contained in the |
| 68 | + * wrapped/inner map |
| 69 | + * @param <OV> |
| 70 | + * the outer value type of the created transforming map, i.e. the type of values appearing to be in this |
| 71 | + * map |
| 72 | + * @param innerKeyTypeToken |
| 73 | + * the token for the inner key type |
| 74 | + * @param outerKeyTypeToken |
| 75 | + * the token for the outer key type |
| 76 | + * @param innerValueTypeToken |
| 77 | + * the token for the inner value type |
| 78 | + * @param outerValueTypeToken |
| 79 | + * the token for the outer value type |
| 80 | + * @return a new builder |
| 81 | + */ |
| 82 | + public static <IK, OK, IV, OV> TransformingMapBuilder<IK, OK, IV, OV> forTypes( |
| 83 | + Class<? super IK> innerKeyTypeToken, Class<? super OK> outerKeyTypeToken, |
| 84 | + Class<? super IV> innerValueTypeToken, Class<? super OV> outerValueTypeToken) { |
| 85 | + |
| 86 | + return new TransformingMapBuilder<>( |
| 87 | + innerKeyTypeToken, outerKeyTypeToken, innerValueTypeToken, outerValueTypeToken); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Creates a new builder for unknown inner and outer key and value types. |
| 92 | + * <p> |
| 93 | + * This is equivalent to calling {@link #forTypes(Class, Class, Class, Class) forTypes(Object.class, Object.class, |
| 94 | + * Object.class, Object.class)}. To obtain a builder for {@code <IK, OK, IV, OV>} you will have to call |
| 95 | + * {@code TransformingMapBuilder.<IK, OK, IV, OV> forTypesUnknown()}. |
| 96 | + * |
| 97 | + * @param <IK> |
| 98 | + * the inner key type of the created transforming map, i.e. the type of the keys contained in the |
| 99 | + * wrapped/inner map |
| 100 | + * @param <OK> |
| 101 | + * the outer key type of the created transforming map, i.e. the type of keys appearing to be in this map |
| 102 | + * @param <IV> |
| 103 | + * the inner value type of the created transforming map, i.e. the type of the values contained in the |
| 104 | + * wrapped/inner map |
| 105 | + * @param <OV> |
| 106 | + * the outer value type of the created transforming map, i.e. the type of values appearing to be in this |
| 107 | + * map |
| 108 | + * @return a new builder |
| 109 | + */ |
| 110 | + public static <IK, OK, IV, OV> TransformingMapBuilder<IK, OK, IV, OV> forTypesUnknown() { |
| 111 | + return forTypes(Object.class, Object.class, Object.class, Object.class); |
| 112 | + } |
| 113 | + |
| 114 | + // #end CONSTRUCTION |
| 115 | + |
| 116 | + // #begin SET FIELDS |
| 117 | + |
| 118 | + /** |
| 119 | + * Sets the transformation from inner to outer keys which will be used by the created map. |
| 120 | + * |
| 121 | + * @param transformToOuterKey |
| 122 | + * transforms inner to outer keys |
| 123 | + * @return this builder |
| 124 | + */ |
| 125 | + public TransformingMapBuilder<IK, OK, IV, OV> toOuterKey(Function<? super IK, ? extends OK> transformToOuterKey) { |
| 126 | + Objects.requireNonNull(transformToOuterKey, "The argument 'transformToOuterKey' must not be null."); |
| 127 | + |
| 128 | + this.transformToOuterKey = transformToOuterKey; |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Sets the transformation from outer to inner keys which will be used by the created map. |
| 134 | + * |
| 135 | + * @param transformToInnerKey |
| 136 | + * transforms outer to inner keys |
| 137 | + * @return this builder |
| 138 | + */ |
| 139 | + public TransformingMapBuilder<IK, OK, IV, OV> toInnerKey(Function<? super OK, ? extends IK> transformToInnerKey) { |
| 140 | + Objects.requireNonNull(transformToInnerKey, "The argument 'transformToInnerKey' must not be null."); |
| 141 | + |
| 142 | + this.transformToInnerKey = transformToInnerKey; |
| 143 | + return this; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Sets the transformation from inner to outer values which will be used by the created map. |
| 148 | + * |
| 149 | + * @param transformToOuterValue |
| 150 | + * transforms inner to outer values |
| 151 | + * @return this builder |
| 152 | + */ |
| 153 | + public TransformingMapBuilder<IK, OK, IV, OV> toOuterValue(Function<? super IV, ? extends OV> transformToOuterValue) { |
| 154 | + Objects.requireNonNull(transformToOuterValue, "The argument 'transformToOuterValue' must not be null."); |
| 155 | + |
| 156 | + this.transformToOuterValue = transformToOuterValue; |
| 157 | + return this; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Sets the transformation from outer to inner values which will be used by the created map. |
| 162 | + * |
| 163 | + * @param transformToInnerValue |
| 164 | + * transforms outer to inner values |
| 165 | + * @return this builder |
| 166 | + */ |
| 167 | + public TransformingMapBuilder<IK, OK, IV, OV> toInnerValue(Function<? super OV, ? extends IV> transformToInnerValue) { |
| 168 | + Objects.requireNonNull(transformToInnerValue, "The argument 'transformToInnerValue' must not be null."); |
| 169 | + |
| 170 | + this.transformToInnerValue = transformToInnerValue; |
| 171 | + return this; |
| 172 | + } |
| 173 | + |
| 174 | + // #end SET FIELDS |
| 175 | + |
| 176 | + // #begin BUILD |
| 177 | + |
| 178 | + /** |
| 179 | + * Creates a {@link TransformingMap} which transforms/decorates the specified map. |
| 180 | + * |
| 181 | + * @param map |
| 182 | + * the map to transform; will be the inner map of the returned transformation |
| 183 | + * @return a new {@link TransformingMap} |
| 184 | + */ |
| 185 | + public TransformingMap<IK, OK, IV, OV> transformMap(Map<IK, IV> map) { |
| 186 | + return new TransformingMap<>(map, |
| 187 | + innerKeyTypeToken, outerKeyTypeToken, transformToOuterKey, transformToInnerKey, |
| 188 | + innerValueTypeToken, outerValueTypeToken, transformToOuterValue, transformToInnerValue); |
| 189 | + } |
| 190 | + |
| 191 | + // #end BUILD |
| 192 | + |
| 193 | +} |
0 commit comments