01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.geronimo.gbean;
17:
18: import java.util.Collection;
19: import java.util.Collections;
20: import java.util.Iterator;
21:
22: /**
23: * A wrapper around a reference collection to simulate a 0..1 reference
24: */
25: public class SingleElementCollection<T> {
26:
27: private final Collection<T> collection;
28:
29: public SingleElementCollection(T element) {
30: if (element == null) {
31: collection = Collections.emptySet();
32: } else {
33: collection = Collections.singleton(element);
34: }
35: }
36:
37: public SingleElementCollection(Collection<T> collection) {
38: if (collection == null) {
39: collection = Collections.emptySet();
40: }
41:
42: this .collection = collection;
43: }
44:
45: public T getElement() {
46: if (collection.isEmpty()) {
47: return null;
48: }
49: if (collection.size() > 1) {
50: throw new IllegalStateException("More than one element: "
51: + collection);
52: }
53: Iterator<T> it = collection.iterator();
54: return it.next();
55: }
56:
57: }
|