|
| 1 | +/* |
| 2 | + * Copyright (c) 2002-2018 "Neo Technology," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.neo4j.driver.internal; |
| 20 | + |
| 21 | +import java.time.Duration; |
| 22 | +import java.time.Period; |
| 23 | +import java.time.temporal.Temporal; |
| 24 | +import java.time.temporal.TemporalUnit; |
| 25 | +import java.time.temporal.UnsupportedTemporalTypeException; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Objects; |
| 28 | + |
| 29 | +import org.neo4j.driver.v1.types.IsoDuration; |
| 30 | + |
| 31 | +import static java.time.temporal.ChronoUnit.DAYS; |
| 32 | +import static java.time.temporal.ChronoUnit.MONTHS; |
| 33 | +import static java.time.temporal.ChronoUnit.NANOS; |
| 34 | +import static java.time.temporal.ChronoUnit.SECONDS; |
| 35 | +import static java.util.Arrays.asList; |
| 36 | +import static java.util.Collections.unmodifiableList; |
| 37 | + |
| 38 | +public class InternalIsoDuration implements IsoDuration |
| 39 | +{ |
| 40 | + private static final List<TemporalUnit> SUPPORTED_UNITS = unmodifiableList( asList( MONTHS, DAYS, SECONDS, NANOS ) ); |
| 41 | + |
| 42 | + private final long months; |
| 43 | + private final long days; |
| 44 | + private final long seconds; |
| 45 | + private final long nanoseconds; |
| 46 | + |
| 47 | + public InternalIsoDuration( Period period ) |
| 48 | + { |
| 49 | + this( period.toTotalMonths(), period.getDays(), 0, 0 ); |
| 50 | + } |
| 51 | + |
| 52 | + public InternalIsoDuration( Duration duration ) |
| 53 | + { |
| 54 | + this( 0, 0, duration.getSeconds(), duration.getNano() ); |
| 55 | + } |
| 56 | + |
| 57 | + public InternalIsoDuration( long months, long days, long seconds, long nanoseconds ) |
| 58 | + { |
| 59 | + this.months = months; |
| 60 | + this.days = days; |
| 61 | + this.seconds = seconds; |
| 62 | + this.nanoseconds = nanoseconds; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public long months() |
| 67 | + { |
| 68 | + return months; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public long days() |
| 73 | + { |
| 74 | + return days; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public long seconds() |
| 79 | + { |
| 80 | + return seconds; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public long nanoseconds() |
| 85 | + { |
| 86 | + return nanoseconds; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public long get( TemporalUnit unit ) |
| 91 | + { |
| 92 | + if ( unit == MONTHS ) |
| 93 | + { |
| 94 | + return months; |
| 95 | + } |
| 96 | + else if ( unit == DAYS ) |
| 97 | + { |
| 98 | + return days; |
| 99 | + } |
| 100 | + else if ( unit == SECONDS ) |
| 101 | + { |
| 102 | + return seconds; |
| 103 | + } |
| 104 | + else if ( unit == NANOS ) |
| 105 | + { |
| 106 | + return nanoseconds; |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + throw new UnsupportedTemporalTypeException( "Unsupported unit: " + unit ); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public List<TemporalUnit> getUnits() |
| 116 | + { |
| 117 | + return SUPPORTED_UNITS; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Temporal addTo( Temporal temporal ) |
| 122 | + { |
| 123 | + if ( months != 0 ) |
| 124 | + { |
| 125 | + temporal = temporal.plus( months, MONTHS ); |
| 126 | + } |
| 127 | + if ( days != 0 ) |
| 128 | + { |
| 129 | + temporal = temporal.plus( days, DAYS ); |
| 130 | + } |
| 131 | + if ( seconds != 0 ) |
| 132 | + { |
| 133 | + temporal = temporal.plus( seconds, SECONDS ); |
| 134 | + } |
| 135 | + if ( nanoseconds != 0 ) |
| 136 | + { |
| 137 | + temporal = temporal.plus( nanoseconds, NANOS ); |
| 138 | + } |
| 139 | + return temporal; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public Temporal subtractFrom( Temporal temporal ) |
| 144 | + { |
| 145 | + if ( months != 0 ) |
| 146 | + { |
| 147 | + temporal = temporal.minus( months, MONTHS ); |
| 148 | + } |
| 149 | + if ( days != 0 ) |
| 150 | + { |
| 151 | + temporal = temporal.minus( days, DAYS ); |
| 152 | + } |
| 153 | + if ( seconds != 0 ) |
| 154 | + { |
| 155 | + temporal = temporal.minus( seconds, SECONDS ); |
| 156 | + } |
| 157 | + if ( nanoseconds != 0 ) |
| 158 | + { |
| 159 | + temporal = temporal.minus( nanoseconds, NANOS ); |
| 160 | + } |
| 161 | + return temporal; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public boolean equals( Object o ) |
| 166 | + { |
| 167 | + if ( this == o ) |
| 168 | + { |
| 169 | + return true; |
| 170 | + } |
| 171 | + if ( o == null || getClass() != o.getClass() ) |
| 172 | + { |
| 173 | + return false; |
| 174 | + } |
| 175 | + InternalIsoDuration that = (InternalIsoDuration) o; |
| 176 | + return months == that.months && |
| 177 | + days == that.days && |
| 178 | + seconds == that.seconds && |
| 179 | + nanoseconds == that.nanoseconds; |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public int hashCode() |
| 184 | + { |
| 185 | + return Objects.hash( months, days, seconds, nanoseconds ); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public String toString() |
| 190 | + { |
| 191 | + return "Duration{" + |
| 192 | + "months=" + months + |
| 193 | + ", days=" + days + |
| 194 | + ", seconds=" + seconds + |
| 195 | + ", nanoseconds=" + nanoseconds + |
| 196 | + '}'; |
| 197 | + } |
| 198 | +} |
0 commit comments