001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Florent Guillaume 018 */ 019package org.nuxeo.ecm.core.api.model; 020 021/** 022 * Value holding a base {@link Long} value and a delta. 023 * <p> 024 * This is used when the actual intent of the value is to be an incremental update to an existing value. 025 * 026 * @since 6.0 027 */ 028public class DeltaLong extends Delta { 029 030 private static final long serialVersionUID = 1L; 031 032 private final long base; 033 034 private final long delta; 035 036 /** 037 * A {@link DeltaLong} with the given base and delta. 038 */ 039 public DeltaLong(long base, long delta) { 040 this.base = base; 041 this.delta = delta; 042 } 043 044 /** 045 * Constructs a {@link DeltaLong} from the given base number and delta, or a {@link Long} if the base is 046 * {@code null}. 047 * <p> 048 * The base number may be a {@link Long} or a {@link DeltaLong}. If it is a {@link DeltaLong} then the returned 049 * value will keep its base and just add deltas. 050 * 051 * @param base the base number 052 * @param delta the delta 053 * @return a new {@link DeltaLong} or {@link Long} 054 */ 055 public static Number deltaOrLong(Number base, long delta) { 056 if (base == null) { 057 return Long.valueOf(delta); 058 } else if (base instanceof Long) { 059 return new DeltaLong(base.longValue(), delta); 060 } else if (base instanceof DeltaLong) { 061 DeltaLong dl = (DeltaLong) base; 062 if (delta == 0) { 063 return dl; 064 } else { 065 return new DeltaLong(dl.getBase(), dl.getDelta() + delta); 066 } 067 } else { 068 throw new IllegalArgumentException(base.getClass().getName()); 069 } 070 } 071 072 @Override 073 public Delta add(Delta other) { 074 if (!(other instanceof DeltaLong)) { 075 throw new IllegalArgumentException("Cannot add " + getClass().getSimpleName() + " and " 076 + other.getClass().getSimpleName()); 077 } 078 return new DeltaLong(base, delta + ((DeltaLong) other).delta); 079 } 080 081 @Override 082 public Number add(Number other) { 083 if (!(other instanceof Long)) { 084 throw new IllegalArgumentException("Cannot add " + getClass().getSimpleName() + " and " 085 + other.getClass().getSimpleName()); 086 } 087 return Long.valueOf(((Long) other).longValue() + delta); 088 } 089 090 // @Override 091 public long getBase() { 092 return base; 093 } 094 095 // @Override 096 public long getDelta() { 097 return delta; 098 } 099 100 @Override 101 public Long getDeltaValue() { 102 return Long.valueOf(delta); 103 } 104 105 @Override 106 public Long getFullValue() { 107 return Long.valueOf(longValue()); 108 } 109 110 @Override 111 public long longValue() { 112 return base + delta; 113 } 114 115 @Override 116 public int intValue() { 117 return (int) longValue(); 118 } 119 120 @Override 121 public float floatValue() { 122 return (float) longValue(); 123 } 124 125 @Override 126 public double doubleValue() { 127 return (double) longValue(); 128 } 129 130 @Override 131 public String toString() { 132 return Long.toString(longValue()); 133 } 134 135 @Override 136 public boolean equals(Object obj) { 137 if (obj instanceof DeltaLong) { 138 DeltaLong dl = (DeltaLong) obj; 139 return base == dl.base && delta == dl.delta; 140 } 141 return false; 142 } 143 144 @Override 145 public int hashCode() { 146 int result = 31 + (int) (base ^ (base >>> 32)); 147 return 31 * result + (int) (delta ^ (delta >>> 32)); 148 } 149 150}