|
| 1 | +/* |
| 2 | + * Copyright 2012 Victor Cordis |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * Please contact the author ( [email protected] ) if you need additional |
| 17 | + * information or have any questions. |
| 18 | + */ |
| 19 | +package net.sourceforge.easyml.marshalling.java.util; |
| 20 | + |
| 21 | +import net.sourceforge.easyml.InvalidFormatException; |
| 22 | +import net.sourceforge.easyml.marshalling.CompositeReader; |
| 23 | +import net.sourceforge.easyml.marshalling.CompositeStrategy; |
| 24 | +import net.sourceforge.easyml.marshalling.CompositeWriter; |
| 25 | +import net.sourceforge.easyml.marshalling.UnmarshalContext; |
| 26 | + |
| 27 | +import java.util.*; |
| 28 | +import java.util.function.Consumer; |
| 29 | + |
| 30 | +/** |
| 31 | + * CollectionsStrategies class contains strategies for {@linkplain List#of()}, {@linkplain Map#of()}, |
| 32 | + * {@linkplain Set#of()} factory methods. |
| 33 | + * Implementations are thread-safe. |
| 34 | + * |
| 35 | + * @author Victor Cordis ( cordis.victor at gmail.com) |
| 36 | + * @version 1.7.1 |
| 37 | + * @since 1.7.1 |
| 38 | + */ |
| 39 | +public final class CollectionsStrategies { |
| 40 | + |
| 41 | + /** |
| 42 | + * Constant defining the value used for the unmodifiable list strategy name. |
| 43 | + */ |
| 44 | + public static final String NAME_UNMODIFIABLE_LIST = "unmodif-lst"; |
| 45 | + /** |
| 46 | + * Constant defining the singleton unmodifiable list instance. |
| 47 | + */ |
| 48 | + public static final CompositeStrategy<List> INSTANCE_UNMODIFIABLE_LIST = new CollectionStrategy<>() { |
| 49 | + |
| 50 | + private static final Class TARGET = Collections.unmodifiableList(new LinkedList<>()).getClass(); |
| 51 | + |
| 52 | + @Override |
| 53 | + public Class target() { |
| 54 | + return TARGET; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public String name() { |
| 59 | + return NAME_UNMODIFIABLE_LIST; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void marshalAttr(List target, CompositeWriter writer) { |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public List unmarshalNew(CompositeReader reader, UnmarshalContext ctx) { |
| 68 | + return new LinkedList(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public List unmarshalInit(List target, CompositeReader reader, UnmarshalContext ctx) throws IllegalAccessException { |
| 73 | + super.unmarshalInit(target, reader, ctx); |
| 74 | + return Collections.unmodifiableList(target); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + /** |
| 79 | + * Constant defining the value used for the unmodifiable random access list strategy name. |
| 80 | + */ |
| 81 | + public static final String NAME_UNMODIFIABLE_LIST_RA = "unmodif-lst-ra"; |
| 82 | + /** |
| 83 | + * Constant defining the singleton unmodifiable random access list instance. |
| 84 | + */ |
| 85 | + public static final CompositeStrategy<List> INSTANCE_UNMODIFIABLE_LIST_RA = new CollectionStrategy<>() { |
| 86 | + |
| 87 | + private static final Class TARGET = Collections.unmodifiableList(Collections.EMPTY_LIST).getClass(); |
| 88 | + |
| 89 | + @Override |
| 90 | + public Class target() { |
| 91 | + return TARGET; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public String name() { |
| 96 | + return NAME_UNMODIFIABLE_LIST_RA; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + @Override |
| 101 | + public List unmarshalNew(CompositeReader reader, UnmarshalContext ctx) { |
| 102 | + try { |
| 103 | + return new ArrayList(Integer.parseInt(reader.elementRequiredAttribute(ATTRIBUTE_SIZE))); |
| 104 | + } catch (NumberFormatException nfx) { |
| 105 | + throw new InvalidFormatException(ctx.readerPositionDescriptor(), nfx); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public List unmarshalInit(List target, CompositeReader reader, UnmarshalContext ctx) throws IllegalAccessException { |
| 111 | + super.unmarshalInit(target, reader, ctx); |
| 112 | + return Collections.unmodifiableList(target); |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + /** |
| 117 | + * Constant defining the value used for the unmodifiable map strategy name. |
| 118 | + */ |
| 119 | + public static final String NAME_UNMODIFIABLE_MAP = "unmodif-map"; |
| 120 | + /** |
| 121 | + * Constant defining the singleton unmodifiable map instance. |
| 122 | + */ |
| 123 | + public static final CompositeStrategy<Map> INSTANCE_UNMODIFIABLE_MAP = new MapStrategy<>() { |
| 124 | + |
| 125 | + private static final Class TARGET = Collections.unmodifiableMap(Collections.EMPTY_MAP).getClass(); |
| 126 | + |
| 127 | + @Override |
| 128 | + public Class target() { |
| 129 | + return TARGET; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public String name() { |
| 134 | + return NAME_UNMODIFIABLE_MAP; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public Map unmarshalNew(CompositeReader reader, UnmarshalContext ctx) { |
| 139 | + try { |
| 140 | + return new HashMap(Integer.parseInt(reader.elementRequiredAttribute(ATTRIBUTE_SIZE))); |
| 141 | + } catch (NumberFormatException nfx) { |
| 142 | + throw new InvalidFormatException(ctx.readerPositionDescriptor(), nfx); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public Map unmarshalInit(Map target, CompositeReader reader, UnmarshalContext ctx) throws IllegalAccessException { |
| 148 | + super.unmarshalInit(target, reader, ctx); |
| 149 | + return Collections.unmodifiableMap(target); |
| 150 | + } |
| 151 | + }; |
| 152 | + |
| 153 | + /** |
| 154 | + * Constant defining the value used for the unmodifiable set strategy name. |
| 155 | + */ |
| 156 | + public static final String NAME_UNMODIFIABLE_SET = "unmodif-set"; |
| 157 | + /** |
| 158 | + * Constant defining the singleton unmodifiable set instance. |
| 159 | + */ |
| 160 | + public static final CompositeStrategy<Set> INSTANCE_UNMODIFIABLE_SET = new CollectionStrategy<>() { |
| 161 | + |
| 162 | + private static final Class TARGET = Collections.unmodifiableSet(Collections.EMPTY_SET).getClass(); |
| 163 | + |
| 164 | + @Override |
| 165 | + public Class target() { |
| 166 | + return TARGET; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public String name() { |
| 171 | + return NAME_UNMODIFIABLE_SET; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public Set unmarshalNew(CompositeReader reader, UnmarshalContext ctx) { |
| 176 | + try { |
| 177 | + return new HashSet(Integer.parseInt(reader.elementRequiredAttribute(ATTRIBUTE_SIZE))); |
| 178 | + } catch (NumberFormatException nfx) { |
| 179 | + throw new InvalidFormatException(ctx.readerPositionDescriptor(), nfx); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public Set unmarshalInit(Set target, CompositeReader reader, UnmarshalContext ctx) throws IllegalAccessException { |
| 185 | + super.unmarshalInit(target, reader, ctx); |
| 186 | + return Collections.unmodifiableSet(target); |
| 187 | + } |
| 188 | + }; |
| 189 | + |
| 190 | + /** |
| 191 | + * Consumes each strategy. |
| 192 | + * |
| 193 | + * @param c consumer |
| 194 | + */ |
| 195 | + public static void forEach(Consumer<CompositeStrategy> c) { |
| 196 | + c.accept(INSTANCE_UNMODIFIABLE_LIST); |
| 197 | + c.accept(INSTANCE_UNMODIFIABLE_MAP); |
| 198 | + c.accept(INSTANCE_UNMODIFIABLE_SET); |
| 199 | + } |
| 200 | + |
| 201 | + private CollectionsStrategies() { |
| 202 | + } |
| 203 | +} |
0 commit comments