/* * Copyright 2014 Goldman Sachs. * * 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 com.gs.collections.impl.math; import java.util.concurrent.atomic.AtomicLong; public final class MutableAtomicLong extends AtomicLong implements Comparable<MutableAtomicLong> { private static final long serialVersionUID = 1L; public MutableAtomicLong(long value) { super(value); } public MutableAtomicLong() { } @Override public boolean equals(Object other) { if (this == other) { return true; } if (other == null || this.getClass() != other.getClass()) { return false; } MutableAtomicLong that = (MutableAtomicLong) other; return this.get() == that.get(); } @Override public int hashCode() { long value = this.get(); return (int) (value ^ (value >>> 32)); } @Override public int compareTo(MutableAtomicLong other) { return Long.compare(this.get(), other.get()); } public MutableAtomicLong add(long number) { this.getAndAdd(number); return this; } public MutableAtomicLong subtract(long number) { while (true) { long current = this.get(); long next = current - number; if (this.compareAndSet(current, next)) { break; } } return this; } public MutableAtomicLong multiply(long number) { while (true) { long current = this.get(); long next = current * number; if (this.compareAndSet(current, next)) { break; } } return this; } public MutableAtomicLong divide(long number) { while (true) { long current = this.get(); long next = current / number; if (this.compareAndSet(current, next)) { break; } } return this; } public MutableAtomicLong min(long number) { while (true) { long current = this.get(); long next = Math.min(current, number); if (this.compareAndSet(current, next)) { break; } } return this; } public MutableAtomicLong max(long number) { while (true) { long current = this.get(); long next = Math.max(current, number); if (this.compareAndSet(current, next)) { break; } } return this; } public MutableAtomicLong abs() { while (true) { long current = this.get(); long next = Math.abs(current); if (this.compareAndSet(current, next)) { break; } } return this; } public Long toLong() { return Long.valueOf(this.get()); } }