You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
4.1 KiB
138 lines
4.1 KiB
package de.memtext.baseobjects.coll; |
|
|
|
import java.io.Serializable; |
|
import java.util.Collection; |
|
import java.util.Iterator; |
|
|
|
import de.memtext.baseobjects.NamedIdObject; |
|
import de.memtext.baseobjects.NamedIdObjectI; |
|
import de.memtext.util.EqualsUtil; |
|
import de.memtext.util.StringUtils; |
|
|
|
/** |
|
* |
|
* @author MB |
|
*/ |
|
public class NamedIdObjectCollection extends NamedObjectCollection implements |
|
Collection, Serializable { |
|
private static final long serialVersionUID = 1; |
|
public NamedIdObjectCollection() { |
|
super(); |
|
} |
|
|
|
/** |
|
* |
|
* @param id |
|
* @return object |
|
* @throws IllegalArgumentException |
|
* if nothing found |
|
*/ |
|
public NamedIdObjectI getById(Object id) { |
|
NamedIdObjectI test, result = null; |
|
for (Iterator it = collect.iterator(); it.hasNext();) { |
|
test = (NamedIdObjectI) it.next(); |
|
if ((id == null && test.getId() == null) |
|
|| (test.getId() != null && test.getId().equals(id))) { |
|
result = test; |
|
break; |
|
} |
|
} |
|
if (result == null) |
|
throw new IllegalArgumentException("No element with id " + id |
|
+ " found!"); |
|
return result; |
|
} |
|
|
|
/** |
|
* like '12','34','343' useful for creating sqls like where x in (...) |
|
* |
|
* @return |
|
*/ |
|
public String getIdsApostropheString() { |
|
StringBuffer result = new StringBuffer(); |
|
NamedIdObjectI test; |
|
for (Iterator it = collect.iterator(); it.hasNext();) { |
|
test = (NamedIdObjectI) it.next(); |
|
result.append("'" + test.getId() + "',"); |
|
} |
|
StringUtils.deleteLastChar(result); |
|
return result.toString(); |
|
} |
|
|
|
public boolean containsItemWithName(String name) { |
|
boolean result = false; |
|
NamedIdObjectI test; |
|
for (Iterator it = collect.iterator(); it.hasNext();) { |
|
test = (NamedIdObjectI) it.next(); |
|
if (test.getName().equals(name)) { |
|
result = true; |
|
break; |
|
} |
|
} |
|
return result; |
|
} |
|
|
|
public boolean containsItemWithId(Object id) { |
|
boolean result = false; |
|
NamedIdObjectI test; |
|
for (Iterator it = collect.iterator(); it.hasNext();) { |
|
test = (NamedIdObjectI) it.next(); |
|
if (EqualsUtil.areEqual(test.getId(),id)) { |
|
result = true; |
|
break; |
|
} |
|
} |
|
return result; |
|
} |
|
|
|
/** |
|
* Checks if the collection contains only the ids given in the param |
|
* collection ids |
|
* |
|
* @param Collection |
|
* ids |
|
* @return |
|
*/ |
|
public boolean consistsOfIds(Collection ids) { |
|
|
|
if (this.size() != ids.size()) |
|
return false; |
|
boolean result = true; |
|
for (Iterator it = ids.iterator(); it.hasNext();) { |
|
Object id = it.next(); |
|
try { |
|
Object dummy = getById(id); |
|
} catch (IllegalArgumentException e) { |
|
result = false; |
|
break; |
|
} |
|
} |
|
return result; |
|
} |
|
|
|
public boolean add(Object o) { |
|
if (o == null) |
|
throw new IllegalArgumentException("can't add null value"); |
|
if (!(o instanceof NamedIdObjectI)) |
|
throw new IllegalArgumentException("only named IdObjects allowed"); |
|
return collect.add(o); |
|
} |
|
|
|
public String toString() { |
|
StringBuffer result = new StringBuffer(size() + " NamedIdObjects: "); |
|
for (Iterator it = this.iterator(); it.hasNext();) { |
|
NamedIdObjectI element = (NamedIdObjectI) it.next(); |
|
result.append(element + " - "); |
|
} |
|
return result.toString(); |
|
} |
|
|
|
/** |
|
* Only shallow copy |
|
*/ |
|
protected Object clone() throws CloneNotSupportedException { |
|
NamedIdObjectCollection c=new NamedIdObjectCollection(); |
|
c.addAll(this); |
|
return c; |
|
} |
|
} |