001 /*
002 * Java Genetic Algorithm Library (jenetics-4.1.0).
003 * Copyright (c) 2007-2018 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020 package io.jenetics.util;
021
022 import static java.util.Objects.requireNonNull;
023
024 import java.io.Serializable;
025 import java.util.Arrays;
026 import java.util.regex.PatternSyntaxException;
027 import java.util.stream.Collector;
028
029 import io.jenetics.internal.collection.Array;
030 import io.jenetics.internal.collection.ArrayISeq;
031 import io.jenetics.internal.collection.CharStore;
032
033 /**
034 * This class is used for holding the valid characters of an
035 * {@link io.jenetics.CharacterGene}. It is not a character sequence in the
036 * classical sense. The characters of this sequence are sorted and doesn't
037 * contain duplicate values, like a set.
038 *
039 * <pre>{@code
040 * final CharSeq cs1 = new CharSeq("abcdeaafg");
041 * final CharSeq cs2 = new CharSeq("gfedcbabb");
042 * assert(cs1.equals(cs2));
043 * }</pre>
044 *
045 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
046 * @since 1.0
047 * @version 2.0
048 */
049 public final class CharSeq
050 extends CharSeqBase
051 implements
052 CharSequence,
053 ISeq<Character>,
054 Comparable<CharSeq>,
055 Serializable
056 {
057 private static final long serialVersionUID = 2L;
058
059 /**
060 * Create a new (distinct) CharSeq from the given {@code characters}. The
061 * given {@link CharSequence} is sorted and duplicate values are removed
062 *
063 * @see #CharSeq(CharSequence)
064 *
065 * @param characters the characters.
066 * @throws NullPointerException if the {@code characters} are {@code null}.
067 */
068 public CharSeq(final char[] characters) {
069 super(distinct(characters.clone()));
070 }
071
072 /**
073 * Create a new (distinct) CharSeq from the given {@code characters}. The
074 * given {@link CharSequence} is sorted and duplicate values are removed.
075 *
076 * @param characters the characters.
077 * @throws NullPointerException if the {@code characters} are {@code null}.
078 */
079 public CharSeq(final CharSequence characters) {
080 super(distinct(toCharArray(characters)));
081 }
082
083 private static char[] toCharArray(final CharSequence characters) {
084 requireNonNull(characters, "Characters");
085
086 final char[] chars = new char[characters.length()];
087 for (int i = chars.length; --i >= 0;) {
088 chars[i] = characters.charAt(i);
089 }
090
091 return chars;
092 }
093
094 private static char[] distinct(final char[] chars) {
095 Arrays.sort(chars);
096
097 int j = 0;
098 for (int i = 1; i < chars.length; ++i) {
099 if (chars[j] != chars[i]) {
100 chars[++j] = chars[i];
101 }
102 }
103
104 final int size = Math.min(chars.length, j + 1);
105 final char[] array = new char[size];
106 System.arraycopy(chars, 0, array, 0, size);
107 return array;
108 }
109
110 @Override
111 public boolean contains(final Object object) {
112 return object instanceof Character && contains((Character)object);
113 }
114
115 /**
116 * Test whether this character set contains the given character {@code c}.
117 *
118 * @param c the character to test.
119 * @return {@code true} if this character set contains the given character,
120 * {@code false} otherwise.
121 * @throws NullPointerException if the given character {@code c} is
122 * {@code null}.
123 */
124 public boolean contains(final Character c) {
125 return contains(c.charValue());
126 }
127
128 /**
129 * Test whether this character set contains the given character {@code c}.
130 *
131 * @param c the character to test.
132 * @return {@code true} if this character set contains the given character,
133 * {@code false} otherwise.
134 */
135 public boolean contains(final char c) {
136 return Arrays.binarySearch(array, c) >= 0;
137 }
138
139 @Override
140 public char charAt(final int index) {
141 return array[index];
142 }
143
144 @Override
145 public int length() {
146 return array.length;
147 }
148
149 @Override
150 public CharSeq subSequence(final int start, final int end) {
151 return new CharSeq(new String(array, start, end - start));
152 }
153
154 /**
155 * Test whether this character set is empty.
156 *
157 * @return {@code true} if this character set is empty, {@code false}
158 * otherwise.
159 */
160 public boolean isEmpty() {
161 return array.length == 0;
162 }
163
164 @Override
165 public int hashCode() {
166 return 17 + 31*Arrays.hashCode(array);
167 }
168
169 @Override
170 public boolean equals(final Object obj) {
171 return obj instanceof CharSeq &&
172 Arrays.equals(((CharSeq)obj).array, array);
173 }
174
175 @Override
176 public int compareTo(final CharSeq set) {
177 int result = 0;
178
179 final int n = Math.min(array.length, set.array.length);
180 for (int i = 0; i < n && result == 0; ++i) {
181 result = array[i] - set.array[i];
182 }
183 if (result == 0) {
184 result = array.length - set.array.length;
185 }
186
187 return result;
188 }
189
190 @Override
191 public String toString() {
192 return new String(array);
193 }
194
195 /**
196 * Expands the character range for the given {@code pattern}. E.g
197 * {@code a-zA-Z0-1} will return a string containing all upper and lower
198 * case characters (from a to z) and all digits form 0 to 9.
199 *
200 * @param pattern the {@code pattern} to expand.
201 * @return the expanded pattern.
202 * @throws PatternSyntaxException if the pattern could not be expanded.
203 * @throws NullPointerException if the pattern is {@code null}.
204 */
205 public static String expand(final CharSequence pattern) {
206 requireNonNull(pattern, "Pattern");
207 final StringBuilder out = new StringBuilder();
208
209 for (int i = 0, n = pattern.length(); i < n; ++i) {
210 if (pattern.charAt(i) == '\\') {
211 ++i;
212 if (i < pattern.length()) {
213 out.append(pattern.charAt(i));
214 }
215 } else if (pattern.charAt(i) == '-') {
216 if (i <= 0 || i >= (pattern.length() - 1)) {
217 throw new PatternSyntaxException(
218 "Dangling range operator '-'", pattern.toString(),
219 pattern.length() - 1
220 );
221 }
222
223 final String range = expand(
224 pattern.charAt(i - 1),
225 pattern.charAt(i + 1)
226 );
227 out.append(range);
228
229 ++i;
230 } else if (i + 1 == n || pattern.charAt(i + 1) != '-') {
231 out.append(pattern.charAt(i));
232 }
233 }
234
235 return out.toString();
236 }
237
238 /**
239 * Expands the characters between {@code a} and {@code b}.
240 *
241 * @param a the start character.
242 * @param b the stop character.
243 * @return the expanded characters.
244 */
245 public static String expand(final char a, final char b) {
246 final StringBuilder out = new StringBuilder();
247
248 if (a < b) {
249 char c = a;
250 while (c <= b) {
251 out.append(c);
252 c = (char) (c + 1);
253 }
254 } else if (a > b) {
255 char c = a;
256 while (c >= b) {
257 out.append(c);
258 c = (char)(c - 1);
259 }
260 }
261
262 return out.toString();
263 }
264
265 /**
266 * Expands the character range for the given {@code pattern}. E.g.
267 * {@code a-zA-Z0-1} will return a string containing all upper and lower
268 * case characters (from a to z) and all digits form 0 to 9.
269 *
270 * @see #expand(CharSequence)
271 *
272 * @param pattern the {@code pattern} to expand.
273 * @return the expanded pattern.
274 * @throws PatternSyntaxException if the pattern could not be expanded.
275 * @throws NullPointerException if the pattern is {@code null}.
276 */
277 public static CharSeq of(final CharSequence pattern) {
278 return new CharSeq(expand(pattern));
279 }
280
281 /**
282 * Expands the characters between {@code a} and {@code b}.
283 *
284 * @see #expand(char, char)
285 *
286 * @param a the start character.
287 * @param b the stop character.
288 * @return the expanded characters.
289 */
290 public static CharSeq of(final char a, final char b) {
291 return new CharSeq(expand(a, b));
292 }
293
294 /**
295 * Helper method for creating a sequence of characters from the given
296 * {@code CharSequence}. The returned sequence will contain all characters
297 * in the original order.
298 *
299 * @param chars the char sequence to convert.
300 * @return a sequence which will contain all given chars in the original
301 * order.
302 */
303 public static ISeq<Character> toISeq(final CharSequence chars) {
304 final MSeq<Character> seq = MSeq.ofLength(chars.length());
305 for (int i = 0; i < chars.length(); ++i) {
306 seq.set(i, chars.charAt(i));
307 }
308
309 return seq.toISeq();
310 }
311
312 public static Collector<Character, ?, CharSeq> toCharSeq() {
313 return Collector.of(
314 StringBuilder::new,
315 StringBuilder::append,
316 (a, b) -> {a.append(b); return a;},
317 CharSeq::new
318 );
319 }
320
321 }
322
323 abstract class CharSeqBase extends ArrayISeq<Character> {
324 private static final long serialVersionUID = 1L;
325
326 final char[] array;
327
328 protected CharSeqBase(final char[] characters) {
329 super(Array.of(CharStore.of(characters)).seal());
330 array = characters;
331 }
332
333 }
|