org.apache.empire.commons.Options Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.empire.commons;
import java.io.Serializable;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.empire.xml.XMLUtil;
import org.w3c.dom.Element;
/**
* This class represents a list of possible values that are valid for a particular field.
* Additionally the class provides a text string describing the value for display purposes.
* The class is implemented as a set of OptionEntry objects
* where the entry value is used as the key for the set and thus must be unique.
*
*/
public class Options extends AbstractSet implements Cloneable, Serializable
{
private static final long serialVersionUID = 1L;
public enum InsertPos
{
Top, Bottom, Sort
}
private static final String EMPTY_STRING = "";
private ArrayList list = new ArrayList();
public Options()
{
// Default constructor
}
public Options(Options other)
{
this.addAll(other);
}
@Override
public Options clone()
{
return new Options(this);
}
public Options(OptionEntry [] entries)
{
for (int i=0; i)
value = ((Entry,?>) value).getKey();
if (value instanceof OptionEntry)
value = ((OptionEntry) value).getValue();
// Find it now
int size = list.size();
for (int i = 0; i < size; i++)
{ // Search List for Index
Object v = list.get(i).getValue();
if (ObjectUtils.compareEqual(value, v))
return i;
}
return -1;
}
protected OptionEntry createOptionEntry(Object value, String text)
{
return new OptionEntry(value, text);
}
public OptionEntry getEntry(Object value)
{
int i = getIndex(value);
return (i >= 0 ? list.get(i) : null);
}
public String get(Object value)
{
int i = getIndex(value);
return (i >= 0 ? list.get(i).getText() : EMPTY_STRING);
}
/**
* Gets the value of the entry at index i
*
* @param i the index
*
* @return the value or null
if not found
*/
public Object getValueAt(int i)
{
return (i>=0 && i=0 && i getValues()
{
HashSet