View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2006, 2009
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-11-05  ebak         Make SBLIM client JSR48 compliant
16   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
17   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
18   */
19  
20  package org.metricshub.wbem.sblim.cimclient.internal.uri;
21  
22  /*-
23   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
24   * WBEM Java Client
25   * ჻჻჻჻჻჻
26   * Copyright 2023 - 2025 MetricsHub
27   * ჻჻჻჻჻჻
28   * Licensed under the Apache License, Version 2.0 (the "License");
29   * you may not use this file except in compliance with the License.
30   * You may obtain a copy of the License at
31   *
32   *      http://www.apache.org/licenses/LICENSE-2.0
33   *
34   * Unless required by applicable law or agreed to in writing, software
35   * distributed under the License is distributed on an "AS IS" BASIS,
36   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37   * See the License for the specific language governing permissions and
38   * limitations under the License.
39   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
40   */
41  
42  import org.metricshub.wbem.javax.cim.CIMDateTime;
43  import org.metricshub.wbem.javax.cim.CIMDateTimeAbsolute;
44  import org.metricshub.wbem.javax.cim.CIMDateTimeInterval;
45  import org.metricshub.wbem.sblim.cimclient.internal.util.MOF;
46  
47  /**
48   * Class DateTimeValue is parses and encapsulates a datetime.
49   *
50   */
51  public class DateTimeValue extends Value implements QuotedValue {
52  	private CIMDateTime iDateTime;
53  
54  	/**
55  	 * datetimeValue = // quoted datetime string
56  	 *
57  	 * @param pStrVal
58  	 *            - the dateTime string in an unquoted form
59  	 * @param pThrow
60  	 * @return <code>Value</code> or <code>null</code> if parsing failed and
61  	 *         <code>pThrow</code> is <code>false</code>
62  	 * @throws IllegalArgumentException
63  	 *             if parsing failed and pThrow is true.
64  	 */
65  	public static Value parse(String pStrVal, boolean pThrow) throws IllegalArgumentException {
66  		CIMDateTime dateTime;
67  		try {
68  			dateTime = new CIMDateTimeAbsolute(pStrVal);
69  		} catch (IllegalArgumentException e0) {
70  			try {
71  				dateTime = new CIMDateTimeInterval(pStrVal);
72  			} catch (IllegalArgumentException e1) {
73  				if (pThrow) {
74  					String msg =
75  						"Value=" +
76  						pStrVal +
77  						"\nFailed to parse as DateTimeAbsolute!:\n" +
78  						e0.getMessage() +
79  						"\nFailed to parse as DateTimeInterval!:\n" +
80  						e1.getMessage();
81  					throw new IllegalArgumentException(msg);
82  				}
83  				return null;
84  			}
85  		}
86  		return new DateTimeValue(dateTime);
87  	}
88  
89  	/**
90  	 * @see #parse(String, boolean)
91  	 * @param pStrVal
92  	 * @return a Value or null if parsing failed.
93  	 */
94  	public static Value parse(String pStrVal) {
95  		return parse(pStrVal, false);
96  	}
97  
98  	private DateTimeValue(CIMDateTime pDateTime) {
99  		this.iDateTime = pDateTime;
100 	}
101 
102 	/**
103 	 * getDateTime
104 	 *
105 	 * @return CIMDateTime
106 	 */
107 	public CIMDateTime getDateTime() {
108 		return this.iDateTime;
109 	}
110 
111 	/**
112 	 * @see java.lang.Object#toString()
113 	 */
114 	@Override
115 	public String toString() {
116 		return this.iDateTime.toString();
117 	}
118 
119 	/**
120 	 * @see QuotedValue#toQuotedString()
121 	 */
122 	public String toQuotedString() {
123 		return "\"" + this.iDateTime.toString() + '"';
124 	}
125 
126 	/**
127 	 * @see Value#getTypeInfo()
128 	 */
129 	@Override
130 	public String getTypeInfo() {
131 		return MOF.DT_DATETIME;
132 	}
133 }