001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util.functions;
011:
012: import java.util.*;
013:
014: /**
015: * Description of the return type of certain function. This wraps a Class object but it has some
016: * extra members. Can be used as a constructor argument of {@link Function} objects or as an
017: * argument of {@link Function#setReturnType}.
018: *
019: * @author Daniel Ockeloen
020: * @author Michiel Meeuwissen
021:
022: * @version $Id: ReturnType.java,v 1.19 2006/10/13 14:22:26 nklasens Exp $
023: * @since MMBase-1.7
024: */
025: public class ReturnType<C> extends Parameter<C> implements
026: java.io.Serializable {
027:
028: /**
029: * The return type of a function that does not return a thing.
030: */
031: public static final ReturnType<Void> VOID = new ReturnType(
032: void.class, "Does not return anything");
033:
034: /**
035: * The return type of a function that returns a String.
036: */
037: public static final ReturnType<String> STRING = new ReturnType(
038: String.class, "String");
039:
040: public static final ReturnType<CharSequence> CHARSEQUENCE = new ReturnType(
041: CharSequence.class, "CharSequence");
042:
043: /**
044: * The return type of a function that returns a Integer.
045: */
046: public static final ReturnType<Integer> INTEGER = new ReturnType(
047: Integer.class, "Integer");
048:
049: /**
050: * The return type of a function that returns a Long.
051: */
052: public static final ReturnType<Long> LONG = new ReturnType(
053: Long.class, "Long");
054:
055: /**
056: * The return type of a function that returns a Double.
057: */
058: public static final ReturnType<Double> DOUBLE = new ReturnType(
059: Double.class, "Double");
060:
061: /**
062: * The return type of a function that returns a Boolean.
063: */
064: public static final ReturnType<Boolean> BOOLEAN = new ReturnType(
065: Boolean.class, "Boolean");
066:
067: /**
068: * The return type of a function that returns a List.
069: */
070: public static final ReturnType LIST = new ReturnType(List.class,
071: "List");
072:
073: /**
074: * The return type of a function that returns a NodeList.
075: */
076: public static final ReturnType<org.mmbase.bridge.NodeList> NODELIST = new ReturnType<org.mmbase.bridge.NodeList>(
077: org.mmbase.bridge.NodeList.class, "NodeList");
078:
079: /**
080: * The return type of a function that returns a Node.
081: */
082: public static final ReturnType<org.mmbase.bridge.Node> NODE = new ReturnType<org.mmbase.bridge.Node>(
083: org.mmbase.bridge.Node.class, "Node");
084:
085: /**
086: * The return type of a function that returns a Set.
087: */
088: public static final ReturnType SET = new ReturnType(Set.class,
089: "Set");
090: /**
091: * The return type of a function that returns a Set.
092: */
093: public static final ReturnType COLLECTION = new ReturnType(
094: Collection.class, "Collection");
095:
096: /**
097: * The return type of a function that returns a Map.
098: */
099: public static final ReturnType MAP = new ReturnType(Map.class,
100: "Map");
101:
102: /**
103: * The return type of a function is unknown.
104: */
105: public static final ReturnType UNKNOWN = new ReturnType(
106: Object.class, "unknown");
107:
108: /**
109: * The return type of a function is None
110: */
111: public static final ReturnType NONE = new ReturnType(Object.class,
112: "none");
113:
114: /**
115: * Can be return by functions that don't want to return anything. (The function framework
116: * requires you to return <em>something</em>).
117: */
118: public static final Object VOID_VALUE = new Object();
119:
120: /**
121: * @since MMBase-1.9
122: */
123: public static final ReturnType<?> getReturnType(Class<?> type) {
124: if (type.equals(void.class)) {
125: return VOID;
126: } else if (type.equals(String.class)) {
127: return STRING;
128: } else if (type.equals(CharSequence.class)) {
129: return CHARSEQUENCE;
130: } else if (type.equals(Integer.class)) {
131: return INTEGER;
132: } else if (type.equals(Long.class)) {
133: return LONG;
134: } else if (type.equals(Double.class)) {
135: return DOUBLE;
136: } else if (type.equals(Boolean.class)) {
137: return BOOLEAN;
138: } else if (type.equals(List.class)) {
139: return LIST;
140: } else if (type.equals(org.mmbase.bridge.NodeList.class)) {
141: return NODELIST;
142: } else if (type.equals(org.mmbase.bridge.Node.class)) {
143: return NODE;
144: } else if (type.equals(Set.class)) {
145: return SET;
146: } else if (type.equals(Collection.class)) {
147: return COLLECTION;
148: } else if (type.equals(Map.class)) {
149: return MAP;
150: } else if (type.equals(Void.class)) {
151: return VOID;
152: } else {
153: return new ReturnType(type, type.getName());
154: }
155: }
156:
157: private Map<String, ReturnType> typeStruct = new HashMap<String, ReturnType>();
158:
159: public ReturnType(Class type, String description) {
160: super ("RETURN_VALUE", type);
161: setDescription(description, null);
162: }
163:
164: public boolean isRequired() {
165: return false;
166: }
167:
168: /**
169: * If the return type is like a map or struct (key-values pairs), then you might want to describe the
170: * types of the values seperately too.
171: */
172: public ReturnType addSubType(String name, ReturnType type) {
173: return typeStruct.put(name, type);
174: }
175:
176: /**
177: * @return Unmodifiable Map containing the 'subtypes' in case the type is Map. An empty Map otherwise.
178: */
179: public Map<String, ReturnType> getSubTypes() {
180: return Collections.unmodifiableMap(typeStruct);
181: }
182:
183: public String toString() {
184: return getDataType().getTypeAsClass().getName();
185: }
186:
187: }
|