View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2006, 2013
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Endre Bak, ebak@de.ibm.com
12   * 
13   * Flag       Date        Prog         Description
14   * -------------------------------------------------------------------------------
15   * 1565892    2006-10-06  ebak         Make SBLIM client JSR48 compliant
16   * 1669961    2006-04-16  lupusalex    CIMTypedElement.getType() =>getDataType()
17   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
18   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
19   * 2797550    2009-06-01  raman_arora  JSR48 compliance - add Java Generics
20   * 2935258    2010-01-22  blaschke-oss Sync up javax.cim.* javadoc with JSR48 1.0.0
21   * 3400209    2011-08-31  blaschke-oss Highlighted Static Analysis (PMD) issues
22   * 3411879    2011-09-20  blaschke-oss TCK: CIM element value must match type
23   *    2716    2013-12-11  blaschke-oss Sync up javax.* javadoc with JSR48 1.0.0 Final V
24   */
25  
26  package org.metricshub.wbem.javax.cim;
27  
28  /*-
29   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
30   * WBEM Java Client
31   * ჻჻჻჻჻჻
32   * Copyright 2023 - 2025 MetricsHub
33   * ჻჻჻჻჻჻
34   * Licensed under the Apache License, Version 2.0 (the "License");
35   * you may not use this file except in compliance with the License.
36   * You may obtain a copy of the License at
37   *
38   *      http://www.apache.org/licenses/LICENSE-2.0
39   *
40   * Unless required by applicable law or agreed to in writing, software
41   * distributed under the License is distributed on an "AS IS" BASIS,
42   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43   * See the License for the specific language governing permissions and
44   * limitations under the License.
45   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
46   */
47  
48  import java.util.Arrays;
49  import org.metricshub.wbem.sblim.cimclient.internal.util.MOF;
50  
51  //Sync'd against JSR48 1.0.0 javadoc (version 1.7.0_03) on Tue Dec 10 07:02:50 EST 2013
52  /**
53   * <code>CIMValuedElement</code> is a base class used by any element that
54   * contains a name, type and value.
55   *
56   * @param <E>
57   *            Type parameter.
58   */
59  public abstract class CIMValuedElement<E> extends CIMTypedElement {
60  	private static final long serialVersionUID = 4234L;
61  
62  	private E iValue;
63  
64  	/**
65  	 * Creates a new CIM element with the given name, type and value.
66  	 *
67  	 * @param pName
68  	 *            The string for the name for this element.
69  	 * @param pType
70  	 *            The data type for this element.
71  	 * @param pValue
72  	 *            The value for this element. <code>null</code> is a valid
73  	 *            value.
74  	 * @throws IllegalArgumentException
75  	 *             If the value does not match the data type.
76  	 */
77  	protected CIMValuedElement(String pName, CIMDataType pType, E pValue) {
78  		super(pName, pType);
79  		if (pType != null && pValue != null) {
80  			CIMDataType valueDataType;
81  			try {
82  				valueDataType = CIMDataType.getDataType(pValue);
83  			} catch (IllegalArgumentException e) {
84  				// Value has unknown data type, cannot validate
85  				valueDataType = null;
86  			}
87  			if (valueDataType != null && valueDataType.getType() != pType.getType()) throw new IllegalArgumentException(
88  				"CIM value does not match type: " + valueDataType.getType() + " != " + pType.getType()
89  			);
90  		}
91  
92  		this.iValue = pValue;
93  	}
94  
95  	/**
96  	 * Compares this object against the specified object. The result is
97  	 * <code>true</code> if and only if the argument is not <code>null</code>
98  	 * and is a <code>CIMValuedElement</code> that represents the same name,
99  	 * type and value as this object.
100 	 *
101 	 * @param pObj
102 	 *            The object to compare with.
103 	 * @return <code>true</code> if the objects are the same; <code>false</code>
104 	 *         otherwise.
105 	 */
106 	@Override
107 	public boolean equals(Object pObj) {
108 		if (!(pObj instanceof CIMValuedElement)) return false;
109 		CIMValuedElement<?> that = (CIMValuedElement<?>) pObj;
110 		if (!super.equals(that)) return false;
111 		if (getDataType().isArray()) {
112 			return Arrays.equals((Object[]) this.iValue, (Object[]) that.iValue);
113 		}
114 		return this.iValue == null ? that.iValue == null : this.iValue.equals(that.iValue);
115 	}
116 
117 	/**
118 	 * Returns the value for this CIM Element.
119 	 *
120 	 * @return The value of the CIM Element. <code>null</code> is a valid value.
121 	 */
122 	public E getValue() {
123 		return this.iValue;
124 	}
125 
126 	/**
127 	 * Returns a hash code value for the CIM valued element. This method is
128 	 * supported for the benefit of hashtables such as those provided by
129 	 * <code>java.util.Hashtable</code>.
130 	 *
131 	 * @return A hash code value for this CIM valued element.
132 	 */
133 	@Override
134 	public int hashCode() {
135 		return toString().hashCode();
136 	}
137 
138 	/**
139 	 * Returns a <code>String</code> representation of the CIM Element. This
140 	 * method is intended to be used only for debugging purposes, and the format
141 	 * of the returned string may vary between implementations. The returned
142 	 * string may be empty but may not be <code>null</code>.
143 	 *
144 	 * @return String representation of this element.
145 	 */
146 	@Override
147 	public String toString() {
148 		return MOF.valuedElement(this, MOF.EMPTY);
149 	}
150 }