001: /*
002: * $Id: List.java,v 1.15 2007/09/18 11:21:04 agoubard Exp $
003: *
004: * Copyright 2003-2007 Orange Nederland Breedband B.V.
005: * See the COPYRIGHT file for redistribution and use restrictions.
006: */
007: package org.xins.common.types.standard;
008:
009: import org.xins.common.MandatoryArgumentChecker;
010: import org.xins.common.types.ItemList;
011: import org.xins.common.types.TypeValueException;
012:
013: /**
014: * Standard type <em>_list</em>.
015: *
016: * @version $Revision: 1.15 $ $Date: 2007/09/18 11:21:04 $
017: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
018: *
019: * @since XINS 1.5.0.
020: */
021: public final class List extends org.xins.common.types.List {
022:
023: /**
024: * The only instance of this class. This field is never <code>null</code>.
025: */
026: public static final List SINGLETON = new List();
027:
028: /**
029: * Constructs a new <code>List</code>.
030: * This constructor is private, the field {@link #SINGLETON} should be
031: * used.
032: */
033: private List() {
034: super ("_list", Text.SINGLETON);
035: }
036:
037: public ItemList createList() {
038: return new Value();
039: }
040:
041: /**
042: * Constructs a <code>List.Value</code> from the specified string
043: * which is guaranteed to be non-<code>null</code>.
044: *
045: * @param string
046: * the string to convert, cannot be <code>null</code>.
047: *
048: * @return
049: * the {@link List.Value} object, never <code>null</code>.
050: *
051: * @throws IllegalArgumentException
052: * if <code>string == null</code>.
053: *
054: * @throws TypeValueException
055: * if the specified string does not represent a valid value for this
056: * type.
057: */
058: public static Value fromStringForRequired(String string)
059: throws IllegalArgumentException, TypeValueException {
060:
061: // Check preconditions
062: MandatoryArgumentChecker.check("string", string);
063:
064: return (Value) SINGLETON.fromString(string);
065: }
066:
067: /**
068: * Constructs a <code>List.Value</code> from the specified string.
069: *
070: * @param string
071: * the string to convert, can be <code>null</code>.
072: *
073: * @return
074: * the {@link List.Value}, or <code>null</code> if
075: * <code>string == null</code>.
076: *
077: * @throws TypeValueException
078: * if the specified string does not represent a valid value for this
079: * type.
080: */
081: public static Value fromStringForOptional(String string)
082: throws TypeValueException {
083: return (Value) SINGLETON.fromString(string);
084: }
085:
086: /**
087: * Converts the specified <code>List.Value</code> to a string.
088: *
089: * @param value
090: * the value to convert, can be <code>null</code>.
091: *
092: * @return
093: * the textual representation of the value, or <code>null</code> if and
094: * only if <code>value == null</code>.
095: */
096: public static String toString(Value value) {
097:
098: // Short-circuit if the argument is null
099: if (value == null) {
100: return null;
101: }
102: return SINGLETON.toString((ItemList) value);
103: }
104:
105: public String getDescription() {
106: return "An ampersand separated list of text.";
107: }
108:
109: /**
110: * Inner class that represents a list of String.
111: */
112: public static final class Value extends ItemList {
113:
114: /**
115: * Add a new element in the list.
116: *
117: * @param value
118: * the new value to add, cannot be <code>null</code>.
119: *
120: * @throws IllegalArgumentException
121: * if <code>value == null</code>.
122: */
123: public void add(String value) throws IllegalArgumentException {
124: MandatoryArgumentChecker.check("value", value);
125: addItem(value);
126: }
127:
128: /**
129: * Get an element from the list.
130: *
131: * @param index
132: * The position of the required element.
133: *
134: * @return
135: * The element at the specified position, cannot be <code>null</code>.
136: */
137: public String get(int index) {
138: return (String) getItem(index);
139: }
140: }
141: }
|