/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.prestosql.spi.type;

import com.fasterxml.jackson.annotation.JsonValue;

import java.util.Objects;

import static io.prestosql.spi.type.TimeType.MAX_PRECISION;
import static io.prestosql.spi.type.TimeWithTimeZoneTypes.normalizePicos;
import static io.prestosql.spi.type.Timestamps.MINUTES_PER_HOUR;
import static io.prestosql.spi.type.Timestamps.PICOSECONDS_PER_HOUR;
import static io.prestosql.spi.type.Timestamps.PICOSECONDS_PER_MINUTE;
import static io.prestosql.spi.type.Timestamps.PICOSECONDS_PER_SECOND;
import static io.prestosql.spi.type.Timestamps.POWERS_OF_TEN;
import static io.prestosql.spi.type.Timestamps.SECONDS_PER_MINUTE;
import static java.lang.Math.abs;
import static java.lang.String.format;

public final class SqlTimeWithTimeZone
{
    private final int precision;
    private final long picos;
    private final int offsetMinutes;

    public static SqlTimeWithTimeZone newInstance(int precision, long picoseconds, int offsetMinutes)
    {
        return new SqlTimeWithTimeZone(precision, picoseconds, offsetMinutes);
    }

    private SqlTimeWithTimeZone(int precision, long picos, int offsetMinutes)
    {
        this.precision = precision;
        this.picos = picos;
        this.offsetMinutes = offsetMinutes;
    }

    public long getPicos()
    {
        return picos;
    }

    public int getOffsetMinutes()
    {
        return offsetMinutes;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        SqlTimeWithTimeZone other = (SqlTimeWithTimeZone) o;
        return precision == other.precision &&
                // TODO the equals here should probably compare precision, picos, offsetMinutes directly, without interpretation
                normalizePicos(picos, offsetMinutes) == normalizePicos(other.picos, other.offsetMinutes);
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(precision, normalizePicos(picos, offsetMinutes));
    }

    @JsonValue
    @Override
    public String toString()
    {
        StringBuilder builder = new StringBuilder();
        builder.append(format(
                "%02d:%02d:%02d",
                picos / PICOSECONDS_PER_HOUR,
                (picos / PICOSECONDS_PER_MINUTE) % MINUTES_PER_HOUR,
                (picos / PICOSECONDS_PER_SECOND) % SECONDS_PER_MINUTE));

        if (precision > 0) {
            long scaledFraction = (picos % PICOSECONDS_PER_SECOND) / POWERS_OF_TEN[MAX_PRECISION - precision];
            builder.append(".");
            builder.append(format("%0" + precision + "d", scaledFraction));
        }
        builder.append(format("%s%02d:%02d", offsetMinutes >= 0 ? '+' : '-', abs(offsetMinutes / 60), abs(offsetMinutes % 60)));

        return builder.toString();
    }
}

@@ -730,82 +730,8 @@
 ON;%0A
-import static io.prestosql.spi.type.TimeWithTimeZoneTypes.normalizePicos;%0A
 impo
@@ -2215,158 +2215,47 @@
     
-// TODO the equals here should probably compare precision, picos, offsetMinutes directly, without interpretation%0A                normalizePicos(picos,
+picos == other.picos &&%0A               
  off
@@ -2268,40 +2268,11 @@
 utes
-)
  ==
- normalizePicos(other.picos,
  oth
@@ -2279,33 +2279,32 @@
 er.offsetMinutes
-)
 ;%0A    %7D%0A%0A    @Ov
@@ -2385,23 +2385,8 @@
 on, 
-normalizePicos(
 pico
@@ -2402,17 +2402,16 @@
 Minutes)
-)
 ;%0A    %7D%0A

Make SqlTimeWithTimeZone#equals value-based