All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.empire.commons.Options Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
/*
 * 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 set = new HashSet(list.size()); for (OptionEntry e : list) set.add(e.getValue()); return set; } /** * Sets or Adds an option at a certain position * * @param value the value object * @param text the text * @param pos the position, see {@link InsertPos} */ public void set(Object value, String text, InsertPos pos) { if (text == null) { // text must not be null! return; } // Find Index int i = getIndex(value); if (i >= 0) { // already present list.get(i).setText(text); } else { // find insert pos int index; if (pos == InsertPos.Top) index = 0; else if (pos == InsertPos.Sort) index = findInsertPos(text); else // bottom is default index = list.size(); // add entry now list.add(index, createOptionEntry(value, text)); } } /** * Sets or adds Adds an option at the bottom * * @param value the value object * @param text the text */ public void set(Object value, String text) { set(value, text, InsertPos.Bottom); } /** * Adds an object, the check for an existing can be skipped for * performance issues (not recommended!) * * @param value the value * @param text the text * @param noCheck set to true to skip testing for an existing key (handle with care!) */ public void add(Object value, String text, boolean noCheck) { if (noCheck) { list.add(createOptionEntry(value, text)); } else { set(value, text); } } @Override public boolean add(OptionEntry option) { if (option.getText() == null) { // text must not be null! return false; } int i = getIndex(option.getValue()); if (i >= 0) list.set(i, option); else list.add(option); return true; } @Override public void clear() { list.clear(); } @Override public boolean contains(Object object) { // Check if exits return (getIndex(object) >= 0); } @Override public boolean isEmpty() { return (list.size() == 0); } @Override public Iterator iterator() { return list.iterator(); } @Override public boolean remove(Object object) { // Check if exits int i = getIndex(object); if (i < 0) return false; // Element not found // remove list.remove(i); return true; } @Override public int size() { return list.size(); } @Override public Object[] toArray() { return list.toArray(); } @Override public String toString() { StringBuilder b = new StringBuilder(); b.append("{"); boolean first = true; for (OptionEntry e : list) { b.append((first) ? "\"" : ",\r\n \""); b.append(StringUtils.toString(e.getValue())); b.append("\":\""); b.append(e.getText()); b.append("\""); first = false; } b.append("}"); return b.toString(); } /** * Adds all these options to the xml element * * @param element the element to add the option tags to * @param flags not used for now */ public void addXml(Element element, long flags) { // add All Options for(OptionEntry e:list){ String value = String.valueOf(e.getValue()); // Create Option Element Element opt = XMLUtil.addElement(element, "option", e.getText()); opt.setAttribute("value", value); } } private int findInsertPos(String text) { int i = 0; for (; i < list.size(); i++) { OptionEntry e = list.get(i); if (text.compareToIgnoreCase(e.getText()) <= 0) break; } return i; } }