All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
src.main.java.com.eva.properties.ListProperties Maven / Gradle / Ivy
Go to download
Advanced properties with object factories, references and inheritance.
/*
* $Id: ListProperties.java 28 2007-02-25 21:04:59Z max $
*
* Copyright (c) 2006-2007 Maximilian Antoni. All rights reserved.
*
* This software is licensed as described in the file LICENSE.txt, which you
* should have received as part of this distribution. The terms are also
* available at http://www.maxantoni.de/projects/eva-properties/license.txt.
*/
package com.eva.properties;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* is a list based properties implementation. Keys addressing a value must be
* numbers.
*
* @author Max Antoni
* @version $Revision: 28 $
*/
class ListProperties extends Properties implements List {
private class ListPropertiesIterator implements ListIterator {
private int index;
ListPropertiesIterator() {
super();
}
ListPropertiesIterator(int inIndex) {
super();
index = inIndex;
}
/*
* @see java.util.ListIterator#add(java.lang.Object)
*/
public void add(Object inValue) {
ListProperties.this.add(inValue);
}
/*
* @see java.util.ListIterator#hasNext()
*/
public boolean hasNext() {
return index < ListProperties.this.size();
}
/*
* @see java.util.ListIterator#hasPrevious()
*/
public boolean hasPrevious() {
return index > 0;
}
/*
* @see java.util.ListIterator#next()
*/
public Object next() {
return ListProperties.this.get(index++);
}
/*
* @see java.util.ListIterator#nextIndex()
*/
public int nextIndex() {
return index;
}
/*
* @see java.util.ListIterator#previous()
*/
public Object previous() {
return ListProperties.this.get(--index);
}
/*
* @see java.util.ListIterator#previousIndex()
*/
public int previousIndex() {
return index - 1;
}
/*
* @see java.util.ListIterator#remove()
*/
public void remove() {
ListProperties.this.remove(--index);
}
/*
* @see java.util.ListIterator#set(java.lang.Object)
*/
public void set(Object inValue) {
ListProperties.this.set(index, inValue);
}
}
private List list;
ListProperties(Properties inParent) {
this(inParent, new ArrayList());
}
ListProperties(Properties inParent, List inList) {
super(inParent);
list = inList;
}
/*
* @see java.util.List#add(int, java.lang.Object)
*/
public void add(int inIndex, Object inValue) {
list.add(inIndex, inValue);
}
/*
* @see java.util.List#add(java.lang.Object)
*/
public boolean add(Object inValue) {
return list.add(inValue);
}
/*
* @see java.util.List#addAll(java.util.Collection)
*/
public boolean addAll(Collection inValues) {
return list.addAll(inValues);
}
/*
* @see java.util.List#addAll(int, java.util.Collection)
*/
public boolean addAll(int inIndex, Collection inValues) {
return list.addAll(inIndex, inValues);
}
/*
* @see java.util.List#clear()
*/
public void clear() {
list.clear();
}
/*
* @see java.util.List#contains(java.lang.Object)
*/
public boolean contains(Object inValue) {
return list.contains(inValue);
}
/*
* @see java.util.List#containsAll(java.util.Collection)
*/
public boolean containsAll(Collection inValues) {
return list.containsAll(inValues);
}
/*
* @see java.util.List#get(int)
*/
public Object get(int inIndex) {
try {
return new Context(this).replace(list.get(inIndex));
}
catch(PropertiesException e) {
Throwable cause = e.getCause();
if(cause == null) {
throw new PropertiesException("Cannot resolve \"" + inIndex
+ "\", " + e.getMessage());
}
throw new PropertiesException(
"Cannot resolve \"" + inIndex + "\":", cause);
}
}
/*
* @see java.util.List#indexOf(java.lang.Object)
*/
public int indexOf(Object inValue) {
return list.indexOf(inValue);
}
/*
* @see java.util.List#isEmpty()
*/
public boolean isEmpty() {
return list.isEmpty();
}
/*
* @see java.util.List#iterator()
*/
public Iterator iterator() {
return new ListPropertiesIterator();
}
/*
* @see java.util.List#lastIndexOf(java.lang.Object)
*/
public int lastIndexOf(Object inValue) {
return list.lastIndexOf(inValue);
}
/*
* @see com.eva.properties.Properties#length()
*/
public int length() {
return list.size();
}
/*
* @see java.util.List#listIterator()
*/
public ListIterator listIterator() {
return new ListPropertiesIterator();
}
/*
* @see java.util.List#listIterator(int)
*/
public ListIterator listIterator(int inIndex) {
return new ListPropertiesIterator(inIndex);
}
/*
* @see java.util.List#remove(int)
*/
public Object remove(int inIndex) {
return list.remove(inIndex);
}
/*
* @see java.util.List#remove(java.lang.Object)
*/
public boolean remove(Object inValue) {
return list.remove(inValue);
}
/*
* @see java.util.List#removeAll(java.util.Collection)
*/
public boolean removeAll(Collection inValues) {
return list.removeAll(inValues);
}
/*
* @see java.util.List#retainAll(java.util.Collection)
*/
public boolean retainAll(Collection inValues) {
return list.retainAll(inValues);
}
/*
* @see java.util.List#set(int, java.lang.Object)
*/
public Object set(int inIndex, Object inValue) {
return list.set(inIndex, inValue);
}
/*
* @see java.util.List#size()
*/
public int size() {
return list.size();
}
/*
* @see java.util.List#subList(int, int)
*/
public List subList(int inFrom, int inLength) {
throw new UnsupportedOperationException();
}
/*
* @see java.util.List#toArray()
*/
public Object[] toArray() {
return list.toArray();
}
/*
* @see java.util.List#toArray(java.lang.Object[])
*/
public Object[] toArray(Object[] inArray) {
return list.toArray(inArray);
}
/*
* @see com.eva.properties.Properties#copy(com.eva.properties.Properties)
*/
Properties copy(Properties inParent) {
ListProperties newProperties = new ListProperties(inParent);
for(Iterator i = list.iterator(); i.hasNext();) {
Object value = i.next();
if(value instanceof Properties) {
newProperties.list
.add(((Properties) value).copy(newProperties));
}
else if(value instanceof Replaceable) {
newProperties.list.add(((Replaceable) value)
.copy(newProperties));
}
else {
newProperties.list.add(value);
}
}
return newProperties;
}
/*
* @see com.eva.properties.Properties#getProperty(
* com.eva.properties.Context, java.lang.String)
*/
Object getProperty(Context inContext, String inKey)
throws PropertiesException {
int p = inKey.indexOf('.');
if(p > 0) {
String localKey = inKey.substring(0, p);
String rightKey = inKey.substring(p + 1);
if(localKey.equals("*")) {
return createJokerList(inContext, rightKey);
}
if(list == null) {
throw new IndexOutOfBoundsException();
}
int index;
try {
index = Integer.parseInt(localKey);
}
catch(NumberFormatException e) {
return null;
}
Object value = list.get(index);
if(value instanceof Properties) {
Properties inner = (Properties) value;
return inner.getProperty(new Context(inContext, inner),
rightKey);
}
}
if(inKey.equals("length")) {
return new Integer(length());
}
if(list == null) {
return null;
}
Object value;
try {
value = list.get(Integer.parseInt(inKey)); // throws IndexOutOfBounds
}
catch(NumberFormatException e) {
return null;
}
if(value == null) {
return null;
}
return inContext.replace(value);
}
/*
* @see com.eva.properties.Properties#putProperty(
* com.eva.properties.Context, java.lang.String, java.lang.Object)
*/
Object putProperty(Context inContext, String inKey, Object inValue) {
int p = inKey.indexOf('.');
int index;
if(p > 0) {
String key = inKey.substring(0, p);
try {
index = Integer.parseInt(inKey);
}
catch(NumberFormatException e) {
throw new PropertiesException(key + " is not a number.");
}
if(index > list.size()) {
throw new PropertiesException("Index out of bounds, " + index
+ " > " + list.size());
}
Object value = list.get(index);
if(value instanceof Replaceable) {
value = inContext.replaceReplaceable((Replaceable) value);
}
if(value instanceof Properties) {
Properties inner = (Properties) value;
return inner.putProperty(new Context(inContext, inner), inKey
.substring(p + 1), inValue);
}
throw new PropertiesException(key
+ " is neither a map nor a list, " + String.valueOf(value));
}
try {
index = Integer.parseInt(inKey);
}
catch(NumberFormatException e) {
throw new PropertiesException("\"" + inKey + "\" is not a number.");
}
while(list.size() <= index) {
list.add(null);
}
return list.set(index, inValue);
}
Object[] toArray(Class inClass) {
Object[] array = (Object[]) Array.newInstance(inClass, list.size());
for(int i = 0; i < array.length; i++) {
array[i] = get(i);
}
return array;
}
/*
* @see com.eva.properties.Properties#write(com.eva.properties.Writer)
*/
void write(Writer inoutWriter) {
inoutWriter.append("[\n");
inoutWriter.increaseIndentation();
for(Iterator i = list.iterator(); i.hasNext();) {
inoutWriter.appendIndentation();
inoutWriter.write(i.next());
}
inoutWriter.decreaseIndentation();
inoutWriter.appendIndentation();
inoutWriter.append("]\n");
}
private ListProperties createJokerList(Context inContext, String rightKey)
throws PropertiesException {
ListProperties l = new ListProperties(this);
for(Iterator i = list.iterator(); i.hasNext();) {
Object value = i.next();
if(!(value instanceof Properties)) {
continue;
}
value = ((Properties) value).getProperty(inContext,
rightKey);
if(value != null) {
l.add(value);
}
}
return l;
}
}