|
/* |
|
* Copyright 2015 Piazza Software Consulting, LLC |
|
* |
|
* 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.piazzaconsulting.couchbase.cas; |
|
|
|
/** |
|
* Utility class for using the CAS capability injected by the EnableCAS |
|
* annotation. |
|
* |
|
* @author Tony Piazza |
|
*/ |
|
public final class CheckAndSetUtil { |
|
private CheckAndSetUtil() { |
|
// prevents instantiation |
|
} |
|
|
|
/** |
|
* Set the CAS value of the target object. |
|
* |
|
* @param target Object reference of target |
|
* @param cas long CAS value |
|
*/ |
|
public static void set(Object target, long cas) { |
|
getObject(target).setCas(cas); |
|
} |
|
|
|
/** |
|
* Get the CAS value of the target object. |
|
* |
|
* @param target Object reference of target |
|
* @return long CAS value |
|
*/ |
|
public static long get(Object obj) { |
|
return getObject(obj).getCas(); |
|
} |
|
|
|
private static CheckAndSet getObject(Object obj) { |
|
if(!(obj instanceof CheckAndSet)) { |
|
throw new IllegalArgumentException("Type does not implement " + |
|
CheckAndSet.class.getName()); |
|
} |
|
return (CheckAndSet)obj; |
|
} |
|
} |