89d2a428f2ed0838cd9e4111c3b74943f7ec89b1
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      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  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.context.test.concepts;
22
23 import java.io.Serializable;
24 import java.util.Calendar;
25 import java.util.Date;
26 import java.util.TimeZone;
27
28 /**
29  * The Class TestContextItem008.
30  */
31 public class TestContextDateItem implements Serializable {
32     private static final long serialVersionUID = -6984963129968805460L;
33
34     private static final int HASH_PRIME_1 = 31;
35     private static final int FOUR_BYTES = 32;
36
37     private long time;
38
39     private int year;
40     private int month;
41     private int day;
42     private int hour;
43     private int minute;
44     private int second;
45     private int milliSecond;
46
47     /**
48      * The Constructor.
49      */
50     public TestContextDateItem() {
51         this(new Date(System.currentTimeMillis()));
52     }
53
54     /**
55      * The Constructor.
56      *
57      * @param dateValue the date value
58      */
59     public TestContextDateItem(final Date dateValue) {
60         if (dateValue != null) {
61             setDateValue(dateValue.getTime());
62         }
63         else {
64             new Date(0);
65         }
66     }
67
68     /**
69      * The Constructor.
70      *
71      * @param time the time
72      */
73     public TestContextDateItem(final long time) {
74         setDateValue(time);
75     }
76
77     /**
78      * Gets the time.
79      *
80      * @return the time
81      */
82     public long getTime() {
83         return time;
84     }
85
86     /**
87      * Gets the year.
88      *
89      * @return the year
90      */
91     public int getYear() {
92         return year;
93     }
94
95     /**
96      * Gets the month.
97      *
98      * @return the month
99      */
100     public int getMonth() {
101         return month;
102     }
103
104     /**
105      * Gets the day.
106      *
107      * @return the day
108      */
109     public int getDay() {
110         return day;
111     }
112
113     /**
114      * Gets the hour.
115      *
116      * @return the hour
117      */
118     public int getHour() {
119         return hour;
120     }
121
122     /**
123      * Gets the minute.
124      *
125      * @return the minute
126      */
127     public int getMinute() {
128         return minute;
129     }
130
131     /**
132      * Gets the second.
133      *
134      * @return the second
135      */
136     public int getSecond() {
137         return second;
138     }
139
140     /**
141      * Gets the milli second.
142      *
143      * @return the milli second
144      */
145     public int getMilliSecond() {
146         return milliSecond;
147     }
148
149     /**
150      * Gets the date value.
151      *
152      * @return the date value
153      */
154     public Date getDateValue() {
155         return new Date(time);
156     }
157
158     /**
159      * Sets the date value.
160      *
161      * @param dateValue the date value
162      */
163     public void setDateValue(final Date dateValue) {
164         if (dateValue != null) {
165             setDateValue(dateValue.getTime());
166         }
167     }
168
169     /**
170      * Sets the date value.
171      *
172      * @param dateValue the date value
173      */
174     public void setDateValue(final long dateValue) {
175         this.time = dateValue;
176
177         final Calendar calendar = Calendar.getInstance();
178         calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
179         calendar.setTimeInMillis(time);
180
181         year = calendar.get(Calendar.YEAR);
182         month = calendar.get(Calendar.MONTH);
183         day = calendar.get(Calendar.DAY_OF_MONTH);
184         hour = calendar.get(Calendar.HOUR);
185         minute = calendar.get(Calendar.MINUTE);
186         second = calendar.get(Calendar.SECOND);
187         milliSecond = calendar.get(Calendar.MILLISECOND);
188     }
189
190
191     /*
192      * (non-Javadoc)
193      *
194      * @see java.lang.Object#hashCode()
195      */
196     @Override
197     public int hashCode() {
198         final int prime = HASH_PRIME_1;
199         int result = 1;
200         result = prime * result + day;
201         result = prime * result + hour;
202         result = prime * result + milliSecond;
203         result = prime * result + minute;
204         result = prime * result + month;
205         result = prime * result + second;
206         result = prime * result + (int) (time ^ (time >>> FOUR_BYTES));
207         result = prime * result + year;
208         return result;
209     }
210
211     /*
212      * (non-Javadoc)
213      *
214      * @see java.lang.Object#equals(java.lang.Object)
215      */
216     @Override
217     public boolean equals(final Object obj) {
218         if (this == obj) {
219             return true;
220         }
221         if (obj == null) {
222             return false;
223         }
224         if (getClass() != obj.getClass()) {
225             return false;
226         }
227         final TestContextDateItem other = (TestContextDateItem) obj;
228         return time == other.time;
229     }
230
231     /*
232      * (non-Javadoc)
233      *
234      * @see java.lang.Object#toString()
235      */
236     @Override
237     public String toString() {
238         return "TestContextItem008 [time=" + time + ", year=" + year + ", month=" + month + ", day=" + day + ", hour="
239                 + hour + ", minute=" + minute + ", second=" + second + ", milliSecond=" + milliSecond + "]";
240     }
241 }