From 195aa4d8f85d70446ad4e45e109bf18da7bbdebd Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Tue, 3 Jun 2014 18:49:02 +0200 Subject: [PATCH] Add StringSet class. Improve StringList. --- src/fr/devinsy/util/StringList.java | 55 ++++ src/fr/devinsy/util/StringSet.java | 447 ++++++++++++++++++++++++++++ 2 files changed, 502 insertions(+) create mode 100644 src/fr/devinsy/util/StringSet.java diff --git a/src/fr/devinsy/util/StringList.java b/src/fr/devinsy/util/StringList.java index 31ab8c0..6d01c4a 100755 --- a/src/fr/devinsy/util/StringList.java +++ b/src/fr/devinsy/util/StringList.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.text.Collator; import java.util.ArrayList; import java.util.Collections; +import java.util.List; /** * This class is a collection of String objects with specific methods. It makes @@ -51,6 +52,26 @@ public class StringList extends ArrayList implements CharSequence super(initialCapacity); } + /** + * + */ + public StringList(final List source) + { + super(); + + if (source != null) + { + // + ensureCapacity(source.size()); + + // + for (String string : source) + { + this.add(string); + } + } + } + /** * */ @@ -150,6 +171,27 @@ public class StringList extends ArrayList implements CharSequence return (result); } + /** + * + */ + public StringList append(final List strings) + { + StringList result; + + if (strings != null) + { + for (String string : strings) + { + this.add(string); + } + } + + result = this; + + // + return (result); + } + /** * */ @@ -277,6 +319,19 @@ public class StringList extends ArrayList implements CharSequence return (result); } + /** + * + */ + public StringList appendln(final List string) + { + StringList result; + + result = this.append(string).appendln(); + + // + return (result); + } + /** * */ diff --git a/src/fr/devinsy/util/StringSet.java b/src/fr/devinsy/util/StringSet.java new file mode 100644 index 0000000..70331a8 --- /dev/null +++ b/src/fr/devinsy/util/StringSet.java @@ -0,0 +1,447 @@ +/** + * Copyright (C) 2014 Christian Pierre MOMON + * + * This file is part of Devinsy-utils. + * + * Devinsy-utils is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Devinsy-utils is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Devinsy-utils. If not, see + */ +package fr.devinsy.util; + +import java.util.HashSet; +import java.util.List; + +/** + * This class is a set of String objects with specific methods. It makes + * possible to build a string without any copy. The goal is to make easier the + * use of set of strings. + */ +public class StringSet extends HashSet +{ + private static final long serialVersionUID = 7421460140821150313L; + + /** + * + */ + public StringSet() + { + super(); + } + + /** + * + */ + public StringSet(final int initialCapacity) + { + super(initialCapacity); + } + + /** + * + */ + public StringSet(final List source) + { + super(); + + if (source != null) + { + // + for (String string : source) + { + this.add(string); + } + } + } + + /** + * + */ + public StringSet(final String[] source) + { + super(); + + if (source != null) + { + // + for (String string : source) + { + this.add(string); + } + } + } + + /** + * + */ + public StringSet(final StringList source) + { + super(); + + if (source != null) + { + // + for (String string : source) + { + this.add(string); + } + } + } + + /** + * + */ + public StringSet(final StringSet source) + { + super(); + + if (source != null) + { + // + for (String string : source) + { + this.add(string); + } + } + } + + /** + * Check null parameter before add. + */ + @Override + public boolean add(final String string) + { + boolean result; + + if (string != null) + { + // + result = super.add(string); + } + else + { + // + result = false; + } + + // + return result; + } + + /** + * + */ + public StringSet append(final StringSet strings) + { + StringSet result; + + if (strings != null) + { + for (String string : strings) + { + this.add(string); + } + } + + result = this; + + // + return result; + } + + /** + * Deep copy and shallow copy have no sense about a list of immutable + * objects. + * + * @return + */ + @Override + public StringSet clone() + { + StringSet result; + + result = new StringSet(size()); + + for (String string : this) + { + result.add(string); + } + + // + return result; + } + + /** + * + */ + public int length() + { + int result = 0; + + for (String string : this) + { + result += string.length(); + } + + // + return result; + } + + /** + * + */ + public StringSet merge() + { + StringSet result; + + String merge = this.toString(); + + clear(); + add(merge); + + // + result = this; + return result; + } + + /** + * Check null parameter before add. + */ + public StringSet pub(final String string) + { + StringSet result; + + if (string != null) + { + this.add(string); + } + + result = this; + + // + return result; + } + + /** + * + */ + public StringSet put(final char character) + { + StringSet result; + + this.add(String.valueOf(character)); + + result = this; + + // + return result; + } + + /** + * + */ + public StringSet put(final double value) + { + StringSet result; + + result = this.put(String.valueOf(value)); + + // + return result; + } + + /** + * + */ + public StringSet put(final Double value) + { + StringSet result; + + if (value != null) + { + this.put(String.valueOf(value)); + } + + result = this; + + // + return result; + } + + /** + * + */ + public StringSet put(final int value) + { + StringSet result; + + result = this.put(String.valueOf(value)); + + // + return result; + } + + /** + * + */ + public StringSet put(final List strings) + { + StringSet result; + + if (strings != null) + { + for (String string : strings) + { + this.put(string); + } + } + + result = this; + + // + return result; + } + + /** + * + */ + public StringSet put(final long value) + { + StringSet result; + + result = this.put(String.valueOf(value)); + + // + return result; + } + + /** + * + */ + public StringSet put(final String string) + { + StringSet result; + + if (string != null) + { + this.add(string); + } + + result = this; + + // + return result; + } + + /** + * + */ + public StringSet put(final String... strings) + { + StringSet result; + + if (strings != null) + { + for (String string : strings) + { + this.put(string); + } + } + + result = this; + + // + return result; + } + + /** + * + */ + public StringSet put(final StringList strings) + { + StringSet result; + + if (strings != null) + { + for (String string : strings) + { + this.put(string); + } + } + + result = this; + + // + return result; + } + + /** + * + */ + public StringSet put(final StringSet strings) + { + StringSet result; + + if (strings != null) + { + for (String string : strings) + { + this.put(string); + } + } + + result = this; + + // + return result; + } + + /** + * + */ + @Override + public String toString() + { + String result; + + StringBuffer buffer = new StringBuffer(length()); + + for (String string : this) + { + buffer.append(string); + } + + result = buffer.toString(); + + // + return result; + } + + /** + * + * @return + */ + public StringList toStringList() + { + StringList result; + + result = new StringList(this.size()); + + for (String string : this) + { + result.append(string); + } + + // + return result; + } +}