Add StringSet class. Improve StringList.

This commit is contained in:
Christian P. MOMON 2014-06-03 18:49:02 +02:00
parent 2c65ae7adc
commit 195aa4d8f8
2 changed files with 502 additions and 0 deletions

View file

@ -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<String> implements CharSequence
super(initialCapacity);
}
/**
*
*/
public StringList(final List<String> source)
{
super();
if (source != null)
{
//
ensureCapacity(source.size());
//
for (String string : source)
{
this.add(string);
}
}
}
/**
*
*/
@ -150,6 +171,27 @@ public class StringList extends ArrayList<String> implements CharSequence
return (result);
}
/**
*
*/
public StringList append(final List<String> 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<String> implements CharSequence
return (result);
}
/**
*
*/
public StringList appendln(final List<String> string)
{
StringList result;
result = this.append(string).appendln();
//
return (result);
}
/**
*
*/

View file

@ -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 <http://www.gnu.org/licenses/>
*/
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<String>
{
private static final long serialVersionUID = 7421460140821150313L;
/**
*
*/
public StringSet()
{
super();
}
/**
*
*/
public StringSet(final int initialCapacity)
{
super(initialCapacity);
}
/**
*
*/
public StringSet(final List<String> 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<String> 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;
}
}