Elffuss BraidHub

kikocis/java-driver public

JAVA-810: Rename DateWithoutTime to LocalDate.
Changesetc968955c6fd842d59c6e98dc2ca231213c1520c8f18f6196f2829b2670577dc4
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-06-12 14:29 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-06-17 22:47 +02:00
Parentsec1d69cf317b

Browse the files at this changeset

26 files changed

modified changelog/README.md +4 −0
@@ -1,5 +1,9 @@
11 ## Changelog
22
3+### 2.2.0-rc2 (in progress)
4+
5+- [improvement] Rename DateWithoutTime to LocalDate (JAVA-810)
6+
37 ### 2.2.0-rc1
48
59 - [new feature] Protocol V4 enum support (JAVA-783)
modified driver-core/src/main/java/com/datastax/driver/core/AbstractAddressableByIndexData.java +1 −1
@@ -76,7 +76,7 @@
7676 return setValue(i, v == null ? null : TypeCodec.TimestampCodec.instance.serialize(v));
7777 }
7878
79- public T setDate(int i, DateWithoutTime v) {
79+ public T setDate(int i, LocalDate v) {
8080 checkType(i, DataType.Name.DATE);
8181 return setValue(i, TypeCodec.DateCodec.instance.serialize(v));
8282 }
modified driver-core/src/main/java/com/datastax/driver/core/AbstractData.java +2 −2
@@ -153,12 +153,12 @@
153153 return wrapped;
154154 }
155155
156- public T setDate(int i, DateWithoutTime v) {
156+ public T setDate(int i, LocalDate v) {
157157 checkType(i, DataType.Name.DATE);
158158 return setValue(i, TypeCodec.DateCodec.instance.serialize(v));
159159 }
160160
161- public T setDate(String name, DateWithoutTime v) {
161+ public T setDate(String name, LocalDate v) {
162162 int[] indexes = getAllIndexesOf(name);
163163 ByteBuffer value = TypeCodec.DateCodec.instance.serialize(v);
164164 for (int i = 0; i < indexes.length; i++) {
modified driver-core/src/main/java/com/datastax/driver/core/AbstractGettableByIndexData.java +1 −1
@@ -188,7 +188,7 @@
188188 * {@inheritDoc}
189189 */
190190 @Override
191- public DateWithoutTime getDate(int i) {
191+ public LocalDate getDate(int i) {
192192 checkType(i, DataType.Name.DATE);
193193
194194 ByteBuffer value = getValue(i);
modified driver-core/src/main/java/com/datastax/driver/core/AbstractGettableData.java +1 −1
@@ -109,7 +109,7 @@
109109 * {@inheritDoc}
110110 */
111111 @Override
112- public DateWithoutTime getDate(String name) {
112+ public LocalDate getDate(String name) {
113113 return getDate(getIndexOf(name));
114114 }
115115
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +4 −4
@@ -533,7 +533,7 @@
533533 * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object.
534534 * @throws InvalidTypeException if value {@code i} is not of type DATE.
535535 */
536- public BoundStatement setDate(int i, DateWithoutTime v) {
536+ public BoundStatement setDate(int i, LocalDate v) {
537537 return wrapper.setDate(i, v);
538538 }
539539
@@ -550,7 +550,7 @@
550550 * @throws InvalidTypeException if (any occurrence of) {@code name} is
551551 * not of type DATE.
552552 */
553- public BoundStatement setDate(String name, DateWithoutTime v) {
553+ public BoundStatement setDate(String name, LocalDate v) {
554554 return wrapper.setDate(name, v);
555555 }
556556
@@ -1303,14 +1303,14 @@
13031303 /**
13041304 * {@inheritDoc}
13051305 */
1306- public DateWithoutTime getDate(int i) {
1306+ public LocalDate getDate(int i) {
13071307 return wrapper.getDate(i);
13081308 }
13091309
13101310 /**
13111311 * {@inheritDoc}
13121312 */
1313- public DateWithoutTime getDate(String name) {
1313+ public LocalDate getDate(String name) {
13141314 return wrapper.getDate(name);
13151315 }
13161316
modified driver-core/src/main/java/com/datastax/driver/core/DataType.java +1 −1
@@ -64,7 +64,7 @@
6464 UDT (48, UDTValue.class, ProtocolVersion.V3),
6565 TUPLE (49, TupleValue.class, ProtocolVersion.V3),
6666 CUSTOM (0, ByteBuffer.class),
67- DATE (17, DateWithoutTime.class, ProtocolVersion.V4),
67+ DATE (17, LocalDate.class, ProtocolVersion.V4),
6868 TIME (18, Long.class, ProtocolVersion.V4),
6969 SMALLINT (19, Short.class, ProtocolVersion.V4),
7070 TINYINT (20, Byte.class, ProtocolVersion.V4);
deleted driver-core/src/main/java/com/datastax/driver/core/DateWithoutTime.java +0 −257
@@ -1,257 +0,0 @@
1-/*
2- * Copyright (C) 2012-2015 DataStax Inc.
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-package com.datastax.driver.core;
17-
18-import java.util.Calendar;
19-import java.util.Date;
20-import java.util.GregorianCalendar;
21-import java.util.TimeZone;
22-import java.util.concurrent.TimeUnit;
23-
24-import static com.google.common.base.Preconditions.checkArgument;
25-
26-/**
27- * A date with no time components, no time zone, in the ISO 8601 calendar.
28- *
29- * Note that ISO 8601 has a number of differences with the default gregorian calendar used in Java:
30- * <ul>
31- * <li>it uses a proleptic gregorian calendar, meaning that it's gregorian indefinitely back in the past (there is no gregorian change);</li>
32- * <li>there is a year 0.</li>
33- * </ul>
34- *
35- * This class implements these differences, so that year/month/day fields match exactly the ones in
36- * CQL string literals.
37- *
38- * @since 2.2
39- */
40-public final class DateWithoutTime {
41-
42- private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
43-
44- private final long millisSinceEpoch;
45- private final int daysSinceEpoch;
46-
47- // This gets initialized lazily if we ever need it. Once set, it is effectively immutable.
48- private volatile GregorianCalendar calendar;
49-
50- private DateWithoutTime(int daysSinceEpoch) {
51- this.daysSinceEpoch = daysSinceEpoch;
52- this.millisSinceEpoch = TimeUnit.DAYS.toMillis(daysSinceEpoch);
53- }
54-
55- /**
56- * Builds a new instance from a number of days since January 1st, 1970 GMT.
57- *
58- * @param daysSinceEpoch the number of days.
59- *
60- * @return the new instance.
61- */
62- public static DateWithoutTime fromDaysSinceEpoch(int daysSinceEpoch) {
63- return new DateWithoutTime(daysSinceEpoch);
64- }
65-
66- /**
67- * Builds a new instance from a number of milliseconds since January 1st, 1970 GMT.
68- *
69- * @param millisSinceEpoch the number of milliseconds. Note that if it does not correspond to a whole number of days,
70- * it will be rounded towards 0.
71- *
72- * @return the new instance.
73- *
74- * @throws IllegalArgumentException if the date is not in the range [-5877641-06-23; 5881580-07-11].
75- */
76- public static DateWithoutTime fromMillisSinceEpoch(long millisSinceEpoch) throws IllegalArgumentException {
77- long daysSinceEpoch = TimeUnit.MILLISECONDS.toDays(millisSinceEpoch);
78- checkArgument(daysSinceEpoch >= Integer.MIN_VALUE && daysSinceEpoch <= Integer.MAX_VALUE,
79- "Date should be in the range [-5877641-06-23; 5881580-07-11]");
80-
81- return new DateWithoutTime((int)daysSinceEpoch);
82- }
83-
84- /**
85- * Builds a new instance from a year/month/day specification.
86- *
87- * This method is not lenient, i.e. '2014-12-32' will not be treated as '2015-01-01', but
88- * instead throw an {@code IllegalArgumentException}.
89- *
90- * @param year the year in ISO format (see {@link DateWithoutTime this class's Javadoc}).
91- * @param month the month. It is 1-based (e.g. 1 for January).
92- * @param dayOfMonth the day of the month.
93- *
94- * @return the new instance.
95- *
96- * @throws IllegalArgumentException if the corresponding date does not exist in the ISO8601
97- * calendar.
98- */
99- public static DateWithoutTime fromYearMonthDay(int year, int month, int dayOfMonth) {
100- int calendarYear = (year <= 0) ? -year + 1 : year;
101- int calendarEra = (year <= 0) ? GregorianCalendar.BC : GregorianCalendar.AD;
102-
103- GregorianCalendar calendar = isoCalendar();
104- // We can't allow leniency because that could mess with our year shift above (for example if the arguments were 0, 12, 32)
105- calendar.setLenient(false);
106- calendar.clear();
107- calendar.set(calendarYear, month - 1, dayOfMonth, 0, 0, 0);
108- calendar.set(Calendar.ERA, calendarEra);
109-
110- DateWithoutTime date = fromMillisSinceEpoch(calendar.getTimeInMillis());
111- date.calendar = calendar;
112- return date;
113- }
114-
115- /**
116- * Returns the number of days since January 1st, 1970 GMT.
117- *
118- * @return the number of days.
119- */
120- public int getDaysSinceEpoch() {
121- return daysSinceEpoch;
122- }
123-
124- /**
125- * Returns the number of milliseconds since January 1st, 1970 GMT.
126- *
127- * @return the number of milliseconds.
128- */
129- public long getMillisSinceEpoch() {
130- return millisSinceEpoch;
131- }
132-
133- /**
134- * Returns the year.
135- *
136- * @return the year.
137- */
138- public int getYear() {
139- GregorianCalendar c = getCalendar();
140- int year = c.get(Calendar.YEAR);
141- if (c.get(Calendar.ERA) == GregorianCalendar.BC)
142- year = -year + 1;
143- return year;
144- }
145-
146- /**
147- * Returns the month.
148- *
149- * @return the month. It is 1-based, e.g. 1 for January.
150- */
151- public int getMonth() {
152- return getCalendar().get(Calendar.MONTH) + 1;
153- }
154-
155- /**
156- * Returns the day in the month.
157- *
158- * @return the day in the month.
159- */
160- public int getDay() {
161- return getCalendar().get(Calendar.DAY_OF_MONTH);
162- }
163-
164- /**
165- * Builds a new instance by adding a number of years.
166- *
167- * @param years the number of years (can be negative).
168- * @return the new instance.
169- *
170- * @throws IllegalArgumentException if the new date is not in the range [-5877641-06-23; 5881580-07-11].
171- */
172- public DateWithoutTime plusYears(int years) {
173- return add(Calendar.YEAR, years);
174- }
175-
176- /**
177- * Builds a new instance by adding a number of months.
178- *
179- * @param months the number of months (can be negative).
180- * @return the new instance.
181- *
182- * @throws IllegalArgumentException if the new date is not in the range [-5877641-06-23; 5881580-07-11].
183- */
184- public DateWithoutTime plusMonths(int months) {
185- return add(Calendar.MONTH, months);
186- }
187-
188- /**
189- * Builds a new instance by adding a number of days.
190- *
191- * @param days the number of days (can be negative).
192- * @return the new instance.
193- *
194- * @throws IllegalArgumentException if the new date is not in the range [-5877641-06-23; 5881580-07-11].
195- */
196- public DateWithoutTime plusDays(int days) {
197- return add(Calendar.DAY_OF_MONTH, days);
198- }
199-
200- private DateWithoutTime add(int field, int amount) {
201- GregorianCalendar newCalendar = isoCalendar();
202- newCalendar.setTimeInMillis(millisSinceEpoch);
203- newCalendar.add(field, amount);
204- DateWithoutTime newDate = fromMillisSinceEpoch(newCalendar.getTimeInMillis());
205- newDate.calendar = newCalendar;
206- return newDate;
207- }
208-
209- @Override
210- public boolean equals(Object o) {
211- if (this == o)
212- return true;
213-
214- if (o instanceof DateWithoutTime) {
215- DateWithoutTime that = (DateWithoutTime)o;
216- return this.daysSinceEpoch == that.daysSinceEpoch;
217- }
218- return false;
219- }
220-
221- @Override
222- public int hashCode() {
223- return daysSinceEpoch;
224- }
225-
226- @Override
227- public String toString() {
228- return String.format("%d-%s-%s", getYear(),
229- pad2(getMonth()),
230- pad2(getDay()));
231- }
232-
233- private static String pad2(int i) {
234- String s = Integer.toString(i);
235- return s.length() == 2 ? s : "0" + s;
236- }
237-
238- private GregorianCalendar getCalendar() {
239- // Two threads can race and both create a calendar. This is not a problem.
240- if (calendar == null) {
241-
242- // Use a local variable to only expose after we're done mutating it.
243- GregorianCalendar tmp = isoCalendar();
244- tmp.setTimeInMillis(millisSinceEpoch);
245-
246- calendar = tmp;
247- }
248- return calendar;
249- }
250-
251- // This matches what Cassandra uses server side (from Joda Time's LocalDate)
252- private static GregorianCalendar isoCalendar() {
253- GregorianCalendar calendar = new GregorianCalendar(UTC);
254- calendar.setGregorianChange(new Date(Long.MIN_VALUE));
255- return calendar;
256- }
257-}
modified driver-core/src/main/java/com/datastax/driver/core/GettableByIndexData.java +2 −2
@@ -123,7 +123,7 @@
123123 * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object.
124124 * @throws InvalidTypeException if value {@code i} is not of type DATE.
125125 */
126- public DateWithoutTime getDate(int i);
126+ public LocalDate getDate(int i);
127127
128128 /**
129129 * Returns the {@code i}th value as a long in nanoseconds since midnight.
@@ -413,4 +413,4 @@
413413 * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object.
414414 */
415415 public Object getObject(int i);
416-}
No newline at the end of the file
416+}
modified driver-core/src/main/java/com/datastax/driver/core/GettableByNameData.java +2 −2
@@ -122,7 +122,7 @@
122122 * @throws IllegalArgumentException if {@code name} is not valid name for this object.
123123 * @throws InvalidTypeException if value {@code name} is not of type DATE.
124124 */
125- public DateWithoutTime getDate(String name);
125+ public LocalDate getDate(String name);
126126
127127 /**
128128 * Returns the value for {@code name} as a long in nanoseconds since midnight.
@@ -412,4 +412,4 @@
412412 * @throws IllegalArgumentException if {@code name} is not a valid name for this object.
413413 */
414414 Object getObject(String name);
415-}
No newline at the end of the file
415+}
added driver-core/src/main/java/com/datastax/driver/core/LocalDate.java +240 −0
@@ -0,0 +1,240 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
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+package com.datastax.driver.core;
17+
18+import java.util.Calendar;
19+import java.util.Date;
20+import java.util.GregorianCalendar;
21+import java.util.TimeZone;
22+import java.util.concurrent.TimeUnit;
23+
24+import static com.google.common.base.Preconditions.checkArgument;
25+
26+/**
27+ * A date with no time components, no time zone, in the ISO 8601 calendar.
28+ *
29+ * Note that ISO 8601 has a number of differences with the default gregorian calendar used in Java:
30+ * <ul>
31+ * <li>it uses a proleptic gregorian calendar, meaning that it's gregorian indefinitely back in the past (there is no gregorian change);</li>
32+ * <li>there is a year 0.</li>
33+ * </ul>
34+ *
35+ * This class implements these differences, so that year/month/day fields match exactly the ones in
36+ * CQL string literals.
37+ *
38+ * @since 2.2
39+ */
40+public final class LocalDate {
41+
42+ private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
43+
44+ private final long millisSinceEpoch;
45+ private final int daysSinceEpoch;
46+
47+ // This gets initialized lazily if we ever need it. Once set, it is effectively immutable.
48+ private volatile GregorianCalendar calendar;
49+
50+ private LocalDate(int daysSinceEpoch) {
51+ this.daysSinceEpoch = daysSinceEpoch;
52+ this.millisSinceEpoch = TimeUnit.DAYS.toMillis(daysSinceEpoch);
53+ }
54+
55+ /**
56+ * Builds a new instance from a number of days since January 1st, 1970 GMT.
57+ *
58+ * @param daysSinceEpoch the number of days.
59+ *
60+ * @return the new instance.
61+ */
62+ public static LocalDate fromDaysSinceEpoch(int daysSinceEpoch) {
63+ return new LocalDate(daysSinceEpoch);
64+ }
65+
66+ /**
67+ * Builds a new instance from a number of milliseconds since January 1st, 1970 GMT.
68+ * Note that if the given number does not correspond to a whole number of days,
69+ * it will be rounded towards 0.
70+ *
71+ * @param millisSinceEpoch the number of milliseconds since January 1st, 1970 GMT.
72+ *
73+ * @return the new instance.
74+ *
75+ * @throws IllegalArgumentException if the date is not in the range [-5877641-06-23; 5881580-07-11].
76+ */
77+ public static LocalDate fromMillisSinceEpoch(long millisSinceEpoch) throws IllegalArgumentException {
78+ long daysSinceEpoch = TimeUnit.MILLISECONDS.toDays(millisSinceEpoch);
79+ checkArgument(daysSinceEpoch >= Integer.MIN_VALUE && daysSinceEpoch <= Integer.MAX_VALUE,
80+ "Date should be in the range [-5877641-06-23; 5881580-07-11]");
81+
82+ return new LocalDate((int)daysSinceEpoch);
83+ }
84+
85+ /**
86+ * Builds a new instance from a year/month/day specification.
87+ *
88+ * This method is not lenient, i.e. '2014-12-32' will not be treated as '2015-01-01', but
89+ * instead throw an {@code IllegalArgumentException}.
90+ *
91+ * @param year the year in ISO format (see {@link LocalDate this class's Javadoc}).
92+ * @param month the month. It is 1-based (e.g. 1 for January).
93+ * @param dayOfMonth the day of the month.
94+ *
95+ * @return the new instance.
96+ *
97+ * @throws IllegalArgumentException if the corresponding date does not exist in the ISO8601
98+ * calendar.
99+ */
100+ public static LocalDate fromYearMonthDay(int year, int month, int dayOfMonth) {
101+ int calendarYear = (year <= 0) ? -year + 1 : year;
102+ int calendarEra = (year <= 0) ? GregorianCalendar.BC : GregorianCalendar.AD;
103+
104+ GregorianCalendar calendar = isoCalendar();
105+ // We can't allow leniency because that could mess with our year shift above (for example if the arguments were 0, 12, 32)
106+ calendar.setLenient(false);
107+ calendar.clear();
108+ calendar.set(calendarYear, month - 1, dayOfMonth, 0, 0, 0);
109+ calendar.set(Calendar.ERA, calendarEra);
110+
111+ LocalDate date = fromMillisSinceEpoch(calendar.getTimeInMillis());
112+ date.calendar = calendar;
113+ return date;
114+ }
115+
116+ /**
117+ * Returns the number of days since January 1st, 1970 GMT.
118+ *
119+ * @return the number of days.
120+ */
121+ public int getDaysSinceEpoch() {
122+ return daysSinceEpoch;
123+ }
124+
125+ /**
126+ * Returns the number of milliseconds since January 1st, 1970 GMT.
127+ *
128+ * @return the number of milliseconds.
129+ */
130+ public long getMillisSinceEpoch() {
131+ return millisSinceEpoch;
132+ }
133+
134+ /**
135+ * Returns the year.
136+ *
137+ * @return the year.
138+ */
139+ public int getYear() {
140+ GregorianCalendar c = getCalendar();
141+ int year = c.get(Calendar.YEAR);
142+ if (c.get(Calendar.ERA) == GregorianCalendar.BC)
143+ year = -year + 1;
144+ return year;
145+ }
146+
147+ /**
148+ * Returns the month.
149+ *
150+ * @return the month. It is 1-based, e.g. 1 for January.
151+ */
152+ public int getMonth() {
153+ return getCalendar().get(Calendar.MONTH) + 1;
154+ }
155+
156+ /**
157+ * Returns the day in the month.
158+ *
159+ * @return the day in the month.
160+ */
161+ public int getDay() {
162+ return getCalendar().get(Calendar.DAY_OF_MONTH);
163+ }
164+
165+ /**
166+ * Return a new {@link LocalDate} with the specified (signed) amount
167+ * of time added to (or subtracted from) the given {@link Calendar} field,
168+ * based on the calendar's rules.
169+ * <p>
170+ * Note that adding any amount to a field smaller than
171+ * {@link Calendar#DAY_OF_MONTH} will remain without effect,
172+ * as this class does not keep time components.
173+ * <p>
174+ * See {@link Calendar} javadocs for more information.
175+ *
176+ * @param field a {@link Calendar} field to modify.
177+ * @param amount the amount of date or time to be added to the field.
178+ * @return a new {@link LocalDate} with the specified (signed) amount
179+ * of time added to (or subtracted from) the given {@link Calendar} field.
180+ *
181+ * @throws IllegalArgumentException if the new date is not in the range [-5877641-06-23; 5881580-07-11].
182+ */
183+ public LocalDate add(int field, int amount) {
184+ GregorianCalendar newCalendar = isoCalendar();
185+ newCalendar.setTimeInMillis(millisSinceEpoch);
186+ newCalendar.add(field, amount);
187+ LocalDate newDate = fromMillisSinceEpoch(newCalendar.getTimeInMillis());
188+ newDate.calendar = newCalendar;
189+ return newDate;
190+ }
191+
192+ @Override
193+ public boolean equals(Object o) {
194+ if (this == o)
195+ return true;
196+
197+ if (o instanceof LocalDate) {
198+ LocalDate that = (LocalDate)o;
199+ return this.daysSinceEpoch == that.daysSinceEpoch;
200+ }
201+ return false;
202+ }
203+
204+ @Override
205+ public int hashCode() {
206+ return daysSinceEpoch;
207+ }
208+
209+ @Override
210+ public String toString() {
211+ return String.format("%d-%s-%s", getYear(),
212+ pad2(getMonth()),
213+ pad2(getDay()));
214+ }
215+
216+ private static String pad2(int i) {
217+ String s = Integer.toString(i);
218+ return s.length() == 2 ? s : "0" + s;
219+ }
220+
221+ private GregorianCalendar getCalendar() {
222+ // Two threads can race and both create a calendar. This is not a problem.
223+ if (calendar == null) {
224+
225+ // Use a local variable to only expose after we're done mutating it.
226+ GregorianCalendar tmp = isoCalendar();
227+ tmp.setTimeInMillis(millisSinceEpoch);
228+
229+ calendar = tmp;
230+ }
231+ return calendar;
232+ }
233+
234+ // This matches what Cassandra uses server side (from Joda Time's LocalDate)
235+ private static GregorianCalendar isoCalendar() {
236+ GregorianCalendar calendar = new GregorianCalendar(UTC);
237+ calendar.setGregorianChange(new Date(Long.MIN_VALUE));
238+ return calendar;
239+ }
240+}
modified driver-core/src/main/java/com/datastax/driver/core/Row.java +2 −2
@@ -236,7 +236,7 @@
236236 * @throws InvalidTypeException if value {@code i} is not of type DATE.
237237 */
238238 @Override
239- public DateWithoutTime getDate(int i);
239+ public LocalDate getDate(int i);
240240
241241 /**
242242 * Returns the value for {@code name} as a date (without time).
@@ -249,7 +249,7 @@
249249 * @throws InvalidTypeException if value {@code name} is not of type DATE.
250250 */
251251 @Override
252- public DateWithoutTime getDate(String name);
252+ public LocalDate getDate(String name);
253253
254254 /**
255255 * Returns the {@code i}th value as a long in nanoseconds since midnight.
modified driver-core/src/main/java/com/datastax/driver/core/SettableByIndexData.java +2 −2
@@ -110,7 +110,7 @@
110110 * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object.
111111 * @throws InvalidTypeException if value {@code i} is not of type DATE.
112112 */
113- public T setDate(int i, DateWithoutTime v);
113+ public T setDate(int i, LocalDate v);
114114
115115 /**
116116 * Set the {@code i}th value to the provided time as a long in nanoseconds since midnight.
@@ -333,4 +333,4 @@
333333 * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object.
334334 */
335335 public T setToNull(int i);
336-}
No newline at the end of the file
336+}
modified driver-core/src/main/java/com/datastax/driver/core/SettableByNameData.java +2 −2
@@ -127,7 +127,7 @@
127127 * @throws InvalidTypeException if (any occurrence of) {@code name} is
128128 * not of type DATE.
129129 */
130- public T setDate(String name, DateWithoutTime v);
130+ public T setDate(String name, LocalDate v);
131131
132132 /**
133133 * Sets the value for (all occurrences of) variable {@code name} to the
@@ -391,4 +391,4 @@
391391 * @throws IllegalArgumentException if {@code name} is not a valid name for this object.
392392 */
393393 public T setToNull(String name);
394-}
No newline at the end of the file
394+}
modified driver-core/src/main/java/com/datastax/driver/core/TypeCodec.java +8 −8
@@ -936,7 +936,7 @@
936936 }
937937 }
938938
939- static class DateCodec extends TypeCodec<DateWithoutTime> {
939+ static class DateCodec extends TypeCodec<LocalDate> {
940940
941941 public static final DateCodec instance = new DateCodec();
942942 private static final Pattern IS_LONG_PATTERN = Pattern.compile("^-?\\d+$");
@@ -948,7 +948,7 @@
948948 }
949949
950950 @Override
951- public DateWithoutTime parse(String value) {
951+ public LocalDate parse(String value) {
952952 if (IS_LONG_PATTERN.matcher(value).matches()) {
953953 // In CQL, numeric DATE literals are longs between 0 and 2^32 - 1, with the epoch in the middle,
954954 // so parse it as a long and re-center at 0
@@ -964,7 +964,7 @@
964964
965965 int days = (int)(cqlLong - EPOCH_AS_CQL_LONG);
966966
967- return DateWithoutTime.fromDaysSinceEpoch(days);
967+ return LocalDate.fromDaysSinceEpoch(days);
968968 }
969969
970970 SimpleDateFormat parser = new SimpleDateFormat(pattern);
@@ -974,25 +974,25 @@
974974 ParsePosition pos = new ParsePosition(0);
975975 Date date = parser.parse(value, pos);
976976 if (date != null && pos.getIndex() == value.length()) {
977- return DateWithoutTime.fromMillisSinceEpoch(date.getTime());
977+ return LocalDate.fromMillisSinceEpoch(date.getTime());
978978 }
979979
980980 throw new InvalidTypeException(String.format("Cannot parse date value from \"%s\"", value));
981981 }
982982
983983 @Override
984- public String format(DateWithoutTime value) {
984+ public String format(LocalDate value) {
985985 return value.toString();
986986 }
987987
988988 @Override
989- public ByteBuffer serialize(DateWithoutTime value) {
989+ public ByteBuffer serialize(LocalDate value) {
990990 return IntCodec.instance.serializeNoBoxing(javaToProtocol(value.getDaysSinceEpoch()));
991991 }
992992
993993 @Override
994- public DateWithoutTime deserialize(ByteBuffer bytes) {
995- return DateWithoutTime.fromDaysSinceEpoch(protocolToJava(IntCodec.instance.deserializeNoBoxing(bytes)));
994+ public LocalDate deserialize(ByteBuffer bytes) {
995+ return LocalDate.fromDaysSinceEpoch(protocolToJava(IntCodec.instance.deserializeNoBoxing(bytes)));
996996 }
997997
998998 // The protocol encodes DATE as an _unsigned_ int with the epoch in the middle of the range (2^31).
modified driver-core/src/test/java/com/datastax/driver/core/Assertions.java +2 −2
@@ -35,7 +35,7 @@
3535 return new DataTypeAssert(type);
3636 }
3737
38- public static DateWithoutTimeAssert assertThat(DateWithoutTime dateWithoutTime) {
39- return new DateWithoutTimeAssert(dateWithoutTime);
38+ public static LocalDateAssert assertThat(LocalDate localDate) {
39+ return new LocalDateAssert(localDate);
4040 }
4141 }
modified driver-core/src/test/java/com/datastax/driver/core/DataTypeTest.java +1 −1
@@ -96,7 +96,7 @@
9696 case TIMESTAMP:
9797 return new Object[]{ new Date(42L), new Date(91294377723L), new Date(-133L) };
9898 case DATE:
99- return new Object[]{ DateWithoutTime.fromDaysSinceEpoch(16071) /* 2014-01-01 */, DateWithoutTime.fromDaysSinceEpoch(0) /* 1970-01-01 */ };
99+ return new Object[]{ LocalDate.fromDaysSinceEpoch(16071) /* 2014-01-01 */, LocalDate.fromDaysSinceEpoch(0) /* 1970-01-01 */ };
100100 case TIME:
101101 return new Object[]{ 54012123450000L /* 15:00:12.123450000 */, 0L };
102102 case BLOB:
deleted driver-core/src/test/java/com/datastax/driver/core/DateWithoutTimeAssert.java +0 −48
@@ -1,48 +0,0 @@
1-/*
2- * Copyright (C) 2012-2015 DataStax Inc.
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-package com.datastax.driver.core;
17-
18-import org.assertj.core.api.AbstractAssert;
19-
20-import static org.assertj.core.api.Assertions.assertThat;
21-
22-public class DateWithoutTimeAssert extends AbstractAssert<DateWithoutTimeAssert, DateWithoutTime> {
23- public DateWithoutTimeAssert(DateWithoutTime actual) {
24- super(actual, DateWithoutTimeAssert.class);
25- }
26-
27- public DateWithoutTimeAssert hasDaysSinceEpoch(int expected) {
28- assertThat(actual.getDaysSinceEpoch()).isEqualTo(expected);
29- return this;
30- }
31-
32- public DateWithoutTimeAssert hasMillisSinceEpoch(long expected) {
33- assertThat(actual.getMillisSinceEpoch()).isEqualTo(expected);
34- return this;
35- }
36-
37- public DateWithoutTimeAssert hasYearMonthDay(int expectedYear, int expectedMonth, int expectedDay) {
38- assertThat(actual.getYear()).isEqualTo(expectedYear);
39- assertThat(actual.getMonth()).isEqualTo(expectedMonth);
40- assertThat(actual.getDay()).isEqualTo(expectedDay);
41- return this;
42- }
43-
44- public DateWithoutTimeAssert hasToString(String expected) {
45- assertThat(actual.toString()).isEqualTo(expected);
46- return this;
47- }
48-}
deleted driver-core/src/test/java/com/datastax/driver/core/DateWithoutTimeTest.java +0 −195
@@ -1,195 +0,0 @@
1-/*
2- * Copyright (C) 2012-2015 DataStax Inc.
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-package com.datastax.driver.core;
17-
18-import java.util.concurrent.TimeUnit;
19-
20-import org.testng.annotations.Test;
21-
22-import static com.datastax.driver.core.Assertions.assertThat;
23-import static com.datastax.driver.core.DateWithoutTime.fromDaysSinceEpoch;
24-import static com.datastax.driver.core.DateWithoutTime.fromMillisSinceEpoch;
25-import static com.datastax.driver.core.DateWithoutTime.fromYearMonthDay;
26-
27-public class DateWithoutTimeTest {
28-
29- @Test(groups = "unit")
30- public void should_build_from_days_since_epoch() {
31- assertThat(fromDaysSinceEpoch(0))
32- .hasMillisSinceEpoch(0)
33- .hasDaysSinceEpoch(0)
34- .hasYearMonthDay(1970, 1, 1)
35- .hasToString("1970-01-01");
36-
37- assertThat(fromDaysSinceEpoch(10))
38- .hasMillisSinceEpoch(TimeUnit.DAYS.toMillis(10))
39- .hasDaysSinceEpoch(10)
40- .hasYearMonthDay(1970, 1, 11)
41- .hasToString("1970-01-11");
42-
43- assertThat(fromDaysSinceEpoch(-10))
44- .hasMillisSinceEpoch(TimeUnit.DAYS.toMillis(-10))
45- .hasDaysSinceEpoch(-10)
46- .hasYearMonthDay(1969, 12, 22)
47- .hasToString("1969-12-22");
48-
49- assertThat(fromDaysSinceEpoch(Integer.MAX_VALUE))
50- .hasMillisSinceEpoch(TimeUnit.DAYS.toMillis(Integer.MAX_VALUE))
51- .hasDaysSinceEpoch(Integer.MAX_VALUE)
52- .hasYearMonthDay(5881580, 7, 11)
53- .hasToString("5881580-07-11");
54-
55- assertThat(fromDaysSinceEpoch(Integer.MIN_VALUE))
56- .hasMillisSinceEpoch(TimeUnit.DAYS.toMillis(Integer.MIN_VALUE))
57- .hasDaysSinceEpoch(Integer.MIN_VALUE)
58- .hasYearMonthDay(-5877641, 6, 23)
59- .hasToString("-5877641-06-23");
60- }
61-
62- @Test(groups = "unit")
63- public void should_build_from_millis_since_epoch() {
64- assertThat(fromMillisSinceEpoch(0))
65- .hasMillisSinceEpoch(0)
66- .hasDaysSinceEpoch(0)
67- .hasYearMonthDay(1970, 1, 1)
68- .hasToString("1970-01-01");
69-
70- // Rounding
71- assertThat(fromMillisSinceEpoch(3600))
72- .hasMillisSinceEpoch(0)
73- .hasDaysSinceEpoch(0)
74- .hasYearMonthDay(1970, 1, 1)
75- .hasToString("1970-01-01");
76- assertThat(fromMillisSinceEpoch(-3600))
77- .hasMillisSinceEpoch(0)
78- .hasDaysSinceEpoch(0)
79- .hasYearMonthDay(1970, 1, 1)
80- .hasToString("1970-01-01");
81-
82- // Bound checks
83- try {
84- fromMillisSinceEpoch(TimeUnit.DAYS.toMillis((long)Integer.MIN_VALUE - 1));
85- Assertions.fail("Expected an IllegalArgumentException");
86- } catch (IllegalArgumentException e) { /*expected*/ }
87- try {
88- fromMillisSinceEpoch(TimeUnit.DAYS.toMillis((long)Integer.MAX_VALUE + 1));
89- Assertions.fail("Expected an IllegalArgumentException");
90- } catch (IllegalArgumentException e) { /*expected*/ }
91- }
92-
93- @Test(groups = "unit")
94- public void should_build_from_year_month_day() {
95- assertThat(fromYearMonthDay(1970, 1, 1))
96- .hasMillisSinceEpoch(0)
97- .hasDaysSinceEpoch(0)
98- .hasYearMonthDay(1970, 1, 1)
99- .hasToString("1970-01-01");
100-
101- // Handling of 0 / negative years
102- assertThat(fromYearMonthDay(1, 1, 1))
103- .hasDaysSinceEpoch(-719162)
104- .hasYearMonthDay(1, 1, 1)
105- .hasToString("1-01-01");
106- assertThat(fromYearMonthDay(0, 1, 1))
107- .hasDaysSinceEpoch(-719162 - 366)
108- .hasYearMonthDay(0, 1, 1)
109- .hasToString("0-01-01");
110- assertThat(fromYearMonthDay(-1, 1, 1))
111- .hasDaysSinceEpoch(-719162 - 366 - 365)
112- .hasYearMonthDay(-1, 1, 1)
113- .hasToString("-1-01-01");
114-
115- // Month/day out of bounds
116- try {
117- fromYearMonthDay(1970, 0, 1);
118- Assertions.fail("Expected an IllegalArgumentException");
119- } catch (IllegalArgumentException e) { /*expected*/ }
120- try {
121- fromYearMonthDay(1970, 13, 1);
122- Assertions.fail("Expected an IllegalArgumentException");
123- } catch (IllegalArgumentException e) { /*expected*/ }
124- try {
125- fromYearMonthDay(1970, 1, 0);
126- Assertions.fail("Expected an IllegalArgumentException");
127- } catch (IllegalArgumentException e) { /*expected*/ }
128- try {
129- fromYearMonthDay(1970, 1, 32);
130- Assertions.fail("Expected an IllegalArgumentException");
131- } catch (IllegalArgumentException e) { /*expected*/ }
132-
133- // Resulting date out of bounds
134- try {
135- fromYearMonthDay(6000000, 1, 1);
136- Assertions.fail("Expected an IllegalArgumentException");
137- } catch (IllegalArgumentException e) { /*expected*/ }
138- try {
139- fromYearMonthDay(-6000000, 1, 1);
140- Assertions.fail("Expected an IllegalArgumentException");
141- } catch (IllegalArgumentException e) { /*expected*/ }
142- }
143-
144- @Test(groups = "unit")
145- public void should_add_and_subtract_years() {
146- assertThat(fromYearMonthDay(1970, 1, 1).plusYears(1))
147- .hasYearMonthDay(1971, 1, 1);
148- assertThat(fromYearMonthDay(1970, 1, 1).plusYears(-1))
149- .hasYearMonthDay(1969, 1, 1);
150- assertThat(fromYearMonthDay(1970, 1, 1).plusYears(-1970))
151- .hasYearMonthDay(0, 1, 1);
152- assertThat(fromYearMonthDay(1970, 1, 1).plusYears(-1971))
153- .hasYearMonthDay(-1, 1, 1);
154- assertThat(fromYearMonthDay(0, 5, 12).plusYears(1))
155- .hasYearMonthDay(1, 5, 12);
156- assertThat(fromYearMonthDay(-1, 5, 12).plusYears(1))
157- .hasYearMonthDay(0, 5, 12);
158- assertThat(fromYearMonthDay(-1, 5, 12).plusYears(2))
159- .hasYearMonthDay(1, 5, 12);
160- }
161-
162- @Test(groups = "unit")
163- public void should_add_and_subtract_months() {
164- assertThat(fromYearMonthDay(1970, 1, 1).plusMonths(2))
165- .hasYearMonthDay(1970, 3, 1);
166- assertThat(fromYearMonthDay(1970, 1, 1).plusMonths(24))
167- .hasYearMonthDay(1972, 1, 1);
168- assertThat(fromYearMonthDay(1970, 1, 1).plusMonths(-5))
169- .hasYearMonthDay(1969, 8, 1);
170- assertThat(fromYearMonthDay(1, 1, 1).plusMonths(-1))
171- .hasYearMonthDay(0, 12, 1);
172- assertThat(fromYearMonthDay(0, 1, 1).plusMonths(-1))
173- .hasYearMonthDay(-1, 12, 1);
174- assertThat(fromYearMonthDay(-1, 12, 1).plusMonths(1))
175- .hasYearMonthDay(0, 1, 1);
176- assertThat(fromYearMonthDay(0, 12, 1).plusMonths(1))
177- .hasYearMonthDay(1, 1, 1);
178- }
179-
180- @Test(groups="unit")
181- public void should_add_and_subtract_days() {
182- assertThat(fromYearMonthDay(1970, 1, 1).plusDays(12))
183- .hasYearMonthDay(1970, 1, 13);
184- assertThat(fromYearMonthDay(1970, 3, 28).plusDays(-40))
185- .hasYearMonthDay(1970, 2, 16);
186- assertThat(fromYearMonthDay(1, 1, 1).plusDays(-2))
187- .hasYearMonthDay(0, 12, 30);
188- assertThat(fromYearMonthDay(0, 1, 1).plusDays(-2))
189- .hasYearMonthDay(-1, 12, 30);
190- assertThat(fromYearMonthDay(-1, 12, 31).plusDays(4))
191- .hasYearMonthDay(0, 1, 4);
192- assertThat(fromYearMonthDay(0, 12, 25).plusDays(14))
193- .hasYearMonthDay(1, 1, 8);
194- }
195-}
added driver-core/src/test/java/com/datastax/driver/core/LocalDateAssert.java +48 −0
@@ -0,0 +1,48 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
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+package com.datastax.driver.core;
17+
18+import org.assertj.core.api.AbstractAssert;
19+
20+import static org.assertj.core.api.Assertions.assertThat;
21+
22+public class LocalDateAssert extends AbstractAssert<LocalDateAssert, LocalDate> {
23+ public LocalDateAssert(LocalDate actual) {
24+ super(actual, LocalDateAssert.class);
25+ }
26+
27+ public LocalDateAssert hasDaysSinceEpoch(int expected) {
28+ assertThat(actual.getDaysSinceEpoch()).isEqualTo(expected);
29+ return this;
30+ }
31+
32+ public LocalDateAssert hasMillisSinceEpoch(long expected) {
33+ assertThat(actual.getMillisSinceEpoch()).isEqualTo(expected);
34+ return this;
35+ }
36+
37+ public LocalDateAssert hasYearMonthDay(int expectedYear, int expectedMonth, int expectedDay) {
38+ assertThat(actual.getYear()).isEqualTo(expectedYear);
39+ assertThat(actual.getMonth()).isEqualTo(expectedMonth);
40+ assertThat(actual.getDay()).isEqualTo(expectedDay);
41+ return this;
42+ }
43+
44+ public LocalDateAssert hasToString(String expected) {
45+ assertThat(actual.toString()).isEqualTo(expected);
46+ return this;
47+ }
48+}
added driver-core/src/test/java/com/datastax/driver/core/LocalDateTest.java +196 −0
@@ -0,0 +1,196 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
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+package com.datastax.driver.core;
17+
18+import java.util.Calendar;
19+import java.util.concurrent.TimeUnit;
20+
21+import org.testng.annotations.Test;
22+
23+import static com.datastax.driver.core.Assertions.assertThat;
24+import static com.datastax.driver.core.LocalDate.fromDaysSinceEpoch;
25+import static com.datastax.driver.core.LocalDate.fromMillisSinceEpoch;
26+import static com.datastax.driver.core.LocalDate.fromYearMonthDay;
27+
28+public class LocalDateTest {
29+
30+ @Test(groups = "unit")
31+ public void should_build_from_days_since_epoch() {
32+ assertThat(fromDaysSinceEpoch(0))
33+ .hasMillisSinceEpoch(0)
34+ .hasDaysSinceEpoch(0)
35+ .hasYearMonthDay(1970, 1, 1)
36+ .hasToString("1970-01-01");
37+
38+ assertThat(fromDaysSinceEpoch(10))
39+ .hasMillisSinceEpoch(TimeUnit.DAYS.toMillis(10))
40+ .hasDaysSinceEpoch(10)
41+ .hasYearMonthDay(1970, 1, 11)
42+ .hasToString("1970-01-11");
43+
44+ assertThat(fromDaysSinceEpoch(-10))
45+ .hasMillisSinceEpoch(TimeUnit.DAYS.toMillis(-10))
46+ .hasDaysSinceEpoch(-10)
47+ .hasYearMonthDay(1969, 12, 22)
48+ .hasToString("1969-12-22");
49+
50+ assertThat(fromDaysSinceEpoch(Integer.MAX_VALUE))
51+ .hasMillisSinceEpoch(TimeUnit.DAYS.toMillis(Integer.MAX_VALUE))
52+ .hasDaysSinceEpoch(Integer.MAX_VALUE)
53+ .hasYearMonthDay(5881580, 7, 11)
54+ .hasToString("5881580-07-11");
55+
56+ assertThat(fromDaysSinceEpoch(Integer.MIN_VALUE))
57+ .hasMillisSinceEpoch(TimeUnit.DAYS.toMillis(Integer.MIN_VALUE))
58+ .hasDaysSinceEpoch(Integer.MIN_VALUE)
59+ .hasYearMonthDay(-5877641, 6, 23)
60+ .hasToString("-5877641-06-23");
61+ }
62+
63+ @Test(groups = "unit")
64+ public void should_build_from_millis_since_epoch() {
65+ assertThat(fromMillisSinceEpoch(0))
66+ .hasMillisSinceEpoch(0)
67+ .hasDaysSinceEpoch(0)
68+ .hasYearMonthDay(1970, 1, 1)
69+ .hasToString("1970-01-01");
70+
71+ // Rounding
72+ assertThat(fromMillisSinceEpoch(3600))
73+ .hasMillisSinceEpoch(0)
74+ .hasDaysSinceEpoch(0)
75+ .hasYearMonthDay(1970, 1, 1)
76+ .hasToString("1970-01-01");
77+ assertThat(fromMillisSinceEpoch(-3600))
78+ .hasMillisSinceEpoch(0)
79+ .hasDaysSinceEpoch(0)
80+ .hasYearMonthDay(1970, 1, 1)
81+ .hasToString("1970-01-01");
82+
83+ // Bound checks
84+ try {
85+ fromMillisSinceEpoch(TimeUnit.DAYS.toMillis((long)Integer.MIN_VALUE - 1));
86+ Assertions.fail("Expected an IllegalArgumentException");
87+ } catch (IllegalArgumentException e) { /*expected*/ }
88+ try {
89+ fromMillisSinceEpoch(TimeUnit.DAYS.toMillis((long)Integer.MAX_VALUE + 1));
90+ Assertions.fail("Expected an IllegalArgumentException");
91+ } catch (IllegalArgumentException e) { /*expected*/ }
92+ }
93+
94+ @Test(groups = "unit")
95+ public void should_build_from_year_month_day() {
96+ assertThat(fromYearMonthDay(1970, 1, 1))
97+ .hasMillisSinceEpoch(0)
98+ .hasDaysSinceEpoch(0)
99+ .hasYearMonthDay(1970, 1, 1)
100+ .hasToString("1970-01-01");
101+
102+ // Handling of 0 / negative years
103+ assertThat(fromYearMonthDay(1, 1, 1))
104+ .hasDaysSinceEpoch(-719162)
105+ .hasYearMonthDay(1, 1, 1)
106+ .hasToString("1-01-01");
107+ assertThat(fromYearMonthDay(0, 1, 1))
108+ .hasDaysSinceEpoch(-719162 - 366)
109+ .hasYearMonthDay(0, 1, 1)
110+ .hasToString("0-01-01");
111+ assertThat(fromYearMonthDay(-1, 1, 1))
112+ .hasDaysSinceEpoch(-719162 - 366 - 365)
113+ .hasYearMonthDay(-1, 1, 1)
114+ .hasToString("-1-01-01");
115+
116+ // Month/day out of bounds
117+ try {
118+ fromYearMonthDay(1970, 0, 1);
119+ Assertions.fail("Expected an IllegalArgumentException");
120+ } catch (IllegalArgumentException e) { /*expected*/ }
121+ try {
122+ fromYearMonthDay(1970, 13, 1);
123+ Assertions.fail("Expected an IllegalArgumentException");
124+ } catch (IllegalArgumentException e) { /*expected*/ }
125+ try {
126+ fromYearMonthDay(1970, 1, 0);
127+ Assertions.fail("Expected an IllegalArgumentException");
128+ } catch (IllegalArgumentException e) { /*expected*/ }
129+ try {
130+ fromYearMonthDay(1970, 1, 32);
131+ Assertions.fail("Expected an IllegalArgumentException");
132+ } catch (IllegalArgumentException e) { /*expected*/ }
133+
134+ // Resulting date out of bounds
135+ try {
136+ fromYearMonthDay(6000000, 1, 1);
137+ Assertions.fail("Expected an IllegalArgumentException");
138+ } catch (IllegalArgumentException e) { /*expected*/ }
139+ try {
140+ fromYearMonthDay(-6000000, 1, 1);
141+ Assertions.fail("Expected an IllegalArgumentException");
142+ } catch (IllegalArgumentException e) { /*expected*/ }
143+ }
144+
145+ @Test(groups = "unit")
146+ public void should_add_and_subtract_years() {
147+ assertThat(fromYearMonthDay(1970, 1, 1).add(Calendar.YEAR, 1))
148+ .hasYearMonthDay(1971, 1, 1);
149+ assertThat(fromYearMonthDay(1970, 1, 1).add(Calendar.YEAR, -1))
150+ .hasYearMonthDay(1969, 1, 1);
151+ assertThat(fromYearMonthDay(1970, 1, 1).add(Calendar.YEAR, -1970))
152+ .hasYearMonthDay(0, 1, 1);
153+ assertThat(fromYearMonthDay(1970, 1, 1).add(Calendar.YEAR, -1971))
154+ .hasYearMonthDay(-1, 1, 1);
155+ assertThat(fromYearMonthDay(0, 5, 12).add(Calendar.YEAR, 1))
156+ .hasYearMonthDay(1, 5, 12);
157+ assertThat(fromYearMonthDay(-1, 5, 12).add(Calendar.YEAR, 1))
158+ .hasYearMonthDay(0, 5, 12);
159+ assertThat(fromYearMonthDay(-1, 5, 12).add(Calendar.YEAR, 2))
160+ .hasYearMonthDay(1, 5, 12);
161+ }
162+
163+ @Test(groups = "unit")
164+ public void should_add_and_subtract_months() {
165+ assertThat(fromYearMonthDay(1970, 1, 1).add(Calendar.MONTH, 2))
166+ .hasYearMonthDay(1970, 3, 1);
167+ assertThat(fromYearMonthDay(1970, 1, 1).add(Calendar.MONTH, 24))
168+ .hasYearMonthDay(1972, 1, 1);
169+ assertThat(fromYearMonthDay(1970, 1, 1).add(Calendar.MONTH, -5))
170+ .hasYearMonthDay(1969, 8, 1);
171+ assertThat(fromYearMonthDay(1, 1, 1).add(Calendar.MONTH, -1))
172+ .hasYearMonthDay(0, 12, 1);
173+ assertThat(fromYearMonthDay(0, 1, 1).add(Calendar.MONTH, -1))
174+ .hasYearMonthDay(-1, 12, 1);
175+ assertThat(fromYearMonthDay(-1, 12, 1).add(Calendar.MONTH, 1))
176+ .hasYearMonthDay(0, 1, 1);
177+ assertThat(fromYearMonthDay(0, 12, 1).add(Calendar.MONTH, 1))
178+ .hasYearMonthDay(1, 1, 1);
179+ }
180+
181+ @Test(groups = "unit")
182+ public void should_add_and_subtract_days() {
183+ assertThat(fromYearMonthDay(1970, 1, 1).add(Calendar.DAY_OF_MONTH, 12))
184+ .hasYearMonthDay(1970, 1, 13);
185+ assertThat(fromYearMonthDay(1970, 3, 28).add(Calendar.DAY_OF_MONTH, -40))
186+ .hasYearMonthDay(1970, 2, 16);
187+ assertThat(fromYearMonthDay(1, 1, 1).add(Calendar.DAY_OF_MONTH, -2))
188+ .hasYearMonthDay(0, 12, 30);
189+ assertThat(fromYearMonthDay(0, 1, 1).add(Calendar.DAY_OF_MONTH, -2))
190+ .hasYearMonthDay(-1, 12, 30);
191+ assertThat(fromYearMonthDay(-1, 12, 31).add(Calendar.DAY_OF_MONTH, 4))
192+ .hasYearMonthDay(0, 1, 4);
193+ assertThat(fromYearMonthDay(0, 12, 25).add(Calendar.DAY_OF_MONTH, 14))
194+ .hasYearMonthDay(1, 1, 8);
195+ }
196+}
modified driver-core/src/test/java/com/datastax/driver/core/PrimitiveTypeSamples.java +1 −1
@@ -57,7 +57,7 @@
5757 .put(DataType.cint(), Integer.MAX_VALUE)
5858 .put(DataType.text(), "text")
5959 .put(DataType.timestamp(), new Date(872835240000L))
60- .put(DataType.date(), DateWithoutTime.fromDaysSinceEpoch(16071))
60+ .put(DataType.date(), LocalDate.fromDaysSinceEpoch(16071))
6161 .put(DataType.time(), 54012123450000L)
6262 .put(DataType.timeuuid(), UUID.fromString("FE2B4360-28C6-11E2-81C1-0800200C9A66"))
6363 .put(DataType.uuid(), UUID.fromString("067e6162-3b6f-4ae2-a171-2470b63dff00"))
modified driver-core/src/test/java/com/datastax/driver/core/TestUtils.java +3 −3
@@ -101,7 +101,7 @@
101101 bs.setTimestamp(name, (Date) value);
102102 break;
103103 case DATE:
104- bs.setDate(name, (DateWithoutTime)value);
104+ bs.setDate(name, (LocalDate)value);
105105 break;
106106 case TIME:
107107 bs.setTime(name, (Long) value);
@@ -219,7 +219,7 @@
219219 case TIMESTAMP:
220220 return new Date(1352288289L);
221221 case DATE:
222- return DateWithoutTime.fromDaysSinceEpoch(0);
222+ return LocalDate.fromDaysSinceEpoch(0);
223223 case TIME:
224224 return 54012123450000L;
225225 case UUID:
@@ -281,7 +281,7 @@
281281 case TIMESTAMP:
282282 return new Date(872835240000L);
283283 case DATE:
284- return DateWithoutTime.fromDaysSinceEpoch(0);
284+ return LocalDate.fromDaysSinceEpoch(0);
285285 case TIME:
286286 return 54012123450000L;
287287 case UUID:
modified driver-core/src/test/java/com/datastax/driver/core/UserTypesTest.java +1 −1
@@ -290,7 +290,7 @@
290290 alldatatypes.setTimestamp(index, ((Date) sampleData));
291291 break;
292292 case DATE:
293- alldatatypes.setDate(index, ((DateWithoutTime)sampleData));
293+ alldatatypes.setDate(index, ((LocalDate)sampleData));
294294 break;
295295 case TIME:
296296 alldatatypes.setTime(index, ((Long) sampleData));
modified driver-mapping/src/main/java/com/datastax/driver/mapping/TypeMappings.java +2 −2
@@ -22,7 +22,7 @@
2222 import java.util.*;
2323
2424 import com.datastax.driver.core.DataType;
25-import com.datastax.driver.core.DateWithoutTime;
25+import com.datastax.driver.core.LocalDate;
2626 import com.datastax.driver.core.TupleValue;
2727 import com.datastax.driver.core.UDTValue;
2828 import com.datastax.driver.mapping.annotations.UDT;
@@ -64,7 +64,7 @@
6464 return DataType.timestamp();
6565 if (UUID.class.isAssignableFrom(klass))
6666 return DataType.uuid();
67- if (DateWithoutTime.class.isAssignableFrom(klass))
67+ if (LocalDate.class.isAssignableFrom(klass))
6868 return DataType.date();
6969
7070 if (Collection.class.isAssignableFrom(klass))
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperPrimitiveTypes22Test.java +11 −11
@@ -16,7 +16,7 @@
1616 package com.datastax.driver.mapping;
1717
1818 import com.datastax.driver.core.CCMBridge;
19-import com.datastax.driver.core.DateWithoutTime;
19+import com.datastax.driver.core.LocalDate;
2020 import com.datastax.driver.core.utils.Bytes;
2121 import com.datastax.driver.core.utils.CassandraVersion;
2222 import com.datastax.driver.mapping.annotations.PartitionKey;
@@ -35,7 +35,7 @@
3535 protected Collection<String> getTableDefinitions() {
3636 return Arrays.asList("CREATE TABLE primitiveTypes22 ("
3737 + "byteBufferCol blob primary key,"
38- + "dateWithoutTimeCol date,"
38+ + "localDateCol date,"
3939 + "timeCol time, timeWrapperCol time,"
4040 + "byteCol tinyint, byteWrapperCol tinyint,"
4141 + "shortCol smallint, shortWrapperCol smallint)");
@@ -44,7 +44,7 @@
4444 @Test(groups = "short")
4545 public void testWriteRead22() throws Exception {
4646 ByteBuffer byteBufferCol = Bytes.fromHexString("0xCAFEBABE");
47- DateWithoutTime dateWithoutTimeCol = DateWithoutTime.fromMillisSinceEpoch(System.currentTimeMillis());
47+ LocalDate localDateCol = LocalDate.fromMillisSinceEpoch(System.currentTimeMillis());
4848 long timeCol = 123456789L;
4949 Long timeWrapperCol = 123456789L;
5050 byte byteCol = 42;
@@ -54,7 +54,7 @@
5454
5555 PrimitiveTypes22 primitiveTypes = new PrimitiveTypes22();
5656 primitiveTypes.setByteBufferCol(byteBufferCol);
57- primitiveTypes.setDateWithoutTimeCol(dateWithoutTimeCol);
57+ primitiveTypes.setLocalDateCol(localDateCol);
5858 primitiveTypes.setTimeCol(timeCol);
5959 primitiveTypes.setTimeWrapperCol(timeWrapperCol);
6060 primitiveTypes.setByteCol(byteCol);
@@ -67,7 +67,7 @@
6767 PrimitiveTypes22 primitiveTypes2 = mapper.get(byteBufferCol);
6868
6969 assertEquals(primitiveTypes2.getByteBufferCol(), byteBufferCol);
70- assertEquals(primitiveTypes2.getDateWithoutTimeCol(), dateWithoutTimeCol);
70+ assertEquals(primitiveTypes2.getLocalDateCol(), localDateCol);
7171 assertEquals(primitiveTypes2.getTimeCol(), timeCol);
7272 assertEquals(primitiveTypes2.getTimeWrapperCol(), timeWrapperCol);
7373 assertEquals(primitiveTypes2.getByteCol(), byteCol);
@@ -81,7 +81,7 @@
8181 public static class PrimitiveTypes22 {
8282 @PartitionKey
8383 private ByteBuffer byteBufferCol;
84- private DateWithoutTime dateWithoutTimeCol;
84+ private LocalDate localDateCol;
8585 private long timeCol;
8686 private Long timeWrapperCol;
8787 private byte byteCol;
@@ -97,12 +97,12 @@
9797 this.byteBufferCol = byteBufferCol;
9898 }
9999
100- public DateWithoutTime getDateWithoutTimeCol() {
101- return dateWithoutTimeCol;
100+ public LocalDate getLocalDateCol() {
101+ return localDateCol;
102102 }
103103
104- public void setDateWithoutTimeCol(DateWithoutTime dateWithoutTimeCol) {
105- this.dateWithoutTimeCol = dateWithoutTimeCol;
104+ public void setLocalDateCol(LocalDate localDateCol) {
105+ this.localDateCol = localDateCol;
106106 }
107107
108108 public long getTimeCol() {
@@ -153,4 +153,4 @@
153153 this.shortWrapperCol = shortWrapperCol;
154154 }
155155 }
156-}
No newline at the end of the file
156+}