Created
August 19, 2015 10:59
-
-
Save deathbeam/25ef88354543c8bc6996 to your computer and use it in GitHub Desktop.
Wix list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Field; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
import org.apache.commons.lang3.reflect.FieldUtils; | |
public static class WixList<T> extends ArrayList<T> { | |
/** | |
* Adds an item if it isnt already present | |
* @param property | |
* @param newItem | |
* @return true if item was sucesfully added, false otherwise | |
*/ | |
public boolean add(String property, T newItem) { | |
if (size() == 0) { | |
add(newItem); | |
return true; | |
} | |
Field field = FieldUtils.getField(newItem.getClass(), property, true); | |
Object comparator; | |
try { | |
comparator = field.get(newItem); | |
} catch (IllegalAccessException e) { | |
return false; | |
} | |
if (get(property, comparator) == null) { | |
add(newItem); | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Check if list contains this item | |
* @param property | |
* @param comparator | |
* @return true if item is in list, false otherwise | |
*/ | |
public boolean contains(String property, Object comparator) { | |
return get(property, comparator) != null; | |
} | |
/** | |
* Get list of all properies with matching name from this list | |
* @param name | |
* @param <T2> | |
* @return list of properies | |
*/ | |
public<T2> List<T2> properties(String name) { | |
List<T2> results = new ArrayList<>(); | |
if (size() == 0) return results; | |
Iterator<T> items = iterator(); | |
while (items.hasNext()) { | |
T item = items.next(); | |
try { | |
Field field = FieldUtils.getField(get(0).getClass(), name, true); | |
if (field != null) { | |
results.add((T2)field.get(item)); | |
} | |
} catch (IllegalAccessException e) { | |
} | |
} | |
return results; | |
} | |
/** | |
* Finds an item by specified property and comparator | |
* @param property | |
* @param comparator | |
* @return <T> instance if item is found, otherwise null | |
*/ | |
public T get(String property, Object comparator) { | |
if (size() == 0) return null; | |
Field field = FieldUtils.getField(get(0).getClass(), property, true); | |
Iterator<T> items = iterator(); | |
while (items.hasNext()) { | |
T item = items.next(); | |
try { | |
if (field.get(item).equals(comparator)) { | |
return item; | |
} | |
} catch (IllegalAccessException e) { | |
} | |
} | |
return null; | |
} | |
/** | |
* Removes an item by specified property and comparator | |
* @param property | |
* @param comparator | |
* @return True if item was succesfully removed, false otherwise | |
*/ | |
public boolean remove(String property, Object comparator) { | |
if (size() == 0) return false; | |
T item = get(property, comparator); | |
if (item == null) return false; | |
remove(item); | |
return true; | |
} | |
/** | |
* Adds an item if it isnt already present | |
* @param property | |
* @param newItem | |
* @return true if item was sucesfully added, false otherwise | |
*/ | |
public boolean replace(String property, T newItem) { | |
if (size() == 0) return false; | |
Field field = FieldUtils.getField(newItem.getClass(), property, true); | |
Object comparator; | |
try { | |
comparator = field.get(newItem); | |
} catch (IllegalAccessException e) { | |
return false; | |
} | |
T item = get(property, comparator); | |
if (item == null) return false; | |
remove(item); | |
add(newItem); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment