001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Florent Guillaume
011 */
012package org.nuxeo.ecm.core.api.model;
013
014/**
015 * Value holding a base {@link Long} value and a delta.
016 * <p>
017 * This is used when the actual intent of the value is to be an incremental update to an existing value.
018 *
019 * @since 6.0
020 */
021public class DeltaLong extends Delta {
022
023    private static final long serialVersionUID = 1L;
024
025    private final long base;
026
027    private final long delta;
028
029    /**
030     * A {@link DeltaLong} with the given base and delta.
031     */
032    public DeltaLong(long base, long delta) {
033        this.base = base;
034        this.delta = delta;
035    }
036
037    /**
038     * Constructs a {@link DeltaLong} from the given base number and delta, or a {@link Long} if the base is
039     * {@code null}.
040     * <p>
041     * The base number may be a {@link Long} or a {@link DeltaLong}. If it is a {@link DeltaLong} then the returned
042     * value will keep its base and just add deltas.
043     *
044     * @param base the base number
045     * @param delta the delta
046     * @return a new {@link DeltaLong} or {@link Long}
047     */
048    public static Number deltaOrLong(Number base, long delta) {
049        if (base == null) {
050            return Long.valueOf(delta);
051        } else if (base instanceof Long) {
052            return new DeltaLong(base.longValue(), delta);
053        } else if (base instanceof DeltaLong) {
054            DeltaLong dl = (DeltaLong) base;
055            if (delta == 0) {
056                return dl;
057            } else {
058                return new DeltaLong(dl.getBase(), dl.getDelta() + delta);
059            }
060        } else {
061            throw new IllegalArgumentException(base.getClass().getName());
062        }
063    }
064
065    @Override
066    public Delta add(Delta other) {
067        if (!(other instanceof DeltaLong)) {
068            throw new IllegalArgumentException("Cannot add " + getClass().getSimpleName() + " and "
069                    + other.getClass().getSimpleName());
070        }
071        return new DeltaLong(base, delta + ((DeltaLong) other).delta);
072    }
073
074    @Override
075    public Number add(Number other) {
076        if (!(other instanceof Long)) {
077            throw new IllegalArgumentException("Cannot add " + getClass().getSimpleName() + " and "
078                    + other.getClass().getSimpleName());
079        }
080        return Long.valueOf(((Long) other).longValue() + delta);
081    }
082
083    // @Override
084    public long getBase() {
085        return base;
086    }
087
088    // @Override
089    public long getDelta() {
090        return delta;
091    }
092
093    @Override
094    public Long getDeltaValue() {
095        return Long.valueOf(delta);
096    }
097
098    @Override
099    public Long getFullValue() {
100        return Long.valueOf(longValue());
101    }
102
103    @Override
104    public long longValue() {
105        return base + delta;
106    }
107
108    @Override
109    public int intValue() {
110        return (int) longValue();
111    }
112
113    @Override
114    public float floatValue() {
115        return (float) longValue();
116    }
117
118    @Override
119    public double doubleValue() {
120        return (double) longValue();
121    }
122
123    @Override
124    public String toString() {
125        return Long.toString(longValue());
126    }
127
128    @Override
129    public boolean equals(Object obj) {
130        if (obj instanceof DeltaLong) {
131            DeltaLong dl = (DeltaLong) obj;
132            return base == dl.base && delta == dl.delta;
133        }
134        return false;
135    }
136
137    @Override
138    public int hashCode() {
139        int result = 31 + (int) (base ^ (base >>> 32));
140        return 31 * result + (int) (delta ^ (delta >>> 32));
141    }
142
143}