View Javadoc

1   /*
2    * Copyright 2006-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.osgi.service.importer.support;
18  
19  import org.springframework.core.enums.StaticLabeledEnum;
20  
21  /**
22   * Enum-like class containing the OSGi importer services cardinality. Indicates
23   * the number of expected matching services and whether the presence is
24   * mandatory or not.
25   * 
26   * @author Costin Leau
27   */
28  public class Cardinality extends StaticLabeledEnum {
29  
30  	private static final long serialVersionUID = 6377096464873348405L;
31  
32  	/**
33  	 * Optional, single cardinality. At most one OSGi service is expected. This
34  	 * cardinality indicates an OSGi service reference proxy.
35  	 */
36  	public static final Cardinality C_0__1 = new Cardinality(0, "0..1");
37  
38  	/**
39  	 * Optional, multiple cardinality. Zero, one or multiple OSGi services are
40  	 * expected. This cardinality indicates an OSGi service managed collection.
41  	 */
42  	public static final Cardinality C_0__N = new Cardinality(1, "0..N");
43  
44  	/**
45  	 * Mandatory, single cardinality. Exactly one OSGi service is expected. This
46  	 * cardinality indicates an OSGi service reference proxy.
47  	 */
48  	public static final Cardinality C_1__1 = new Cardinality(2, "1..1");
49  
50  	/**
51  	 * Mandatory, multiple cardinality. At least one OSGi service is expected.
52  	 * This cardinality indicates an OSGi service managed collection.
53  	 */
54  	public static final Cardinality C_1__N = new Cardinality(3, "1..N");
55  
56  
57  	/**
58  	 * Indicates if this cardinality implies that at most one service is
59  	 * expected.
60  	 * 
61  	 * @return true if the given cardinality is single, false otherwise
62  	 */
63  	public boolean isSingle() {
64  		return Cardinality.C_0__1.equals(this) || Cardinality.C_1__1.equals(this);
65  	}
66  
67  	/**
68  	 * Indicates if this cardinality implies that multiple services are
69  	 * expected.
70  	 * 
71  	 * @return true if this cardinality is multiple, false otherwise
72  	 */
73  	public boolean isMultiple() {
74  		return Cardinality.C_0__N.equals(this) || Cardinality.C_1__N.equals(this);
75  	}
76  
77  	/**
78  	 * Indicates if this cardinality implies that at least one service is
79  	 * expected (mandatory cardinality).
80  	 * 
81  	 * @return true if this cardinality is mandatory, false otherwise
82  	 */
83  	public boolean isMandatory() {
84  		return Cardinality.C_1__1.equals(this) || Cardinality.C_1__N.equals(this);
85  	}
86  
87  	/**
88  	 * Indicates if this cardinality implies that is acceptable for no matching
89  	 * services to be found.
90  	 * 
91  	 * @return true if this cardinality is optional, false otherwise
92  	 */
93  	public boolean isOptional() {
94  		return Cardinality.C_0__N.equals(this) || Cardinality.C_0__1.equals(this);
95  	}
96  
97  	/**
98  	 * Constructs a new <code>Cardinality</code> instance.
99  	 * 
100 	 * @param code
101 	 * @param label
102 	 */
103 	private Cardinality(int code, String label) {
104 		super(code, label);
105 	}
106 }