com.backendless.Counters Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-sdk-common Show documentation
Show all versions of java-sdk-common Show documentation
Provides access to Backendless API
The newest version!
/*
* ********************************************************************************************************************
*
* BACKENDLESS.COM CONFIDENTIAL
*
* ********************************************************************************************************************
*
* Copyright 2012 BACKENDLESS.COM. All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains the property of Backendless.com and its suppliers,
* if any. The intellectual and technical concepts contained herein are proprietary to Backendless.com and its
* suppliers and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret
* or copyright law. Dissemination of this information or reproduction of this material is strictly forbidden
* unless prior written permission is obtained from Backendless.com.
*
* ********************************************************************************************************************
*/
package com.backendless;
import com.backendless.async.callback.AsyncCallback;
import com.backendless.atomic.AtomicCallback;
import com.backendless.exceptions.BackendlessFault;
import com.backendless.exceptions.ExceptionMessage;
import com.backendless.utils.ResponderHelper;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public final class Counters
{
protected static final String ATOMIC_MANAGER_SERVER_ALIAS = "com.backendless.services.redis.AtomicOperationService";
private static final Counters instance = new Counters();
public static Counters getInstance()
{
return instance;
}
private Counters()
{
}
public static Number convertToType( Number value, Class type )
{
if( type == Short.class )
return value.shortValue();
else if( type == Integer.class )
return value.intValue();
else if( type == Long.class )
return value.longValue();
else if( type == Float.class )
return value.floatValue();
else if( type == Double.class )
return value.doubleValue();
else if( type == Byte.class )
return value.byteValue();
else
throw new RuntimeException( "unsupported data type. Cannot adapt counter value to type - " + type );
}
public IAtomic of( String counterName )
{
if( counterName == null )
throw new IllegalArgumentException( ExceptionMessage.NULL_ENTITY );
return of( counterName, Long.class );
}
public IAtomic of( String counterName, Class extends T> type )
{
return new AtomicCounterImpl<>( this, counterName, type );
}
public void reset( String counterName )
{
Invoker.invokeSync( ATOMIC_MANAGER_SERVER_ALIAS, "reset", new Object[] { counterName } );
}
public void reset( String counterName, AsyncCallback callback )
{
Invoker.invokeAsync( ATOMIC_MANAGER_SERVER_ALIAS, "reset", new Object[] { counterName }, callback );
}
public Long get( String counterName )
{
return runGetOperation( "get", counterName );
}
public void get( String counterName, AsyncCallback responder )
{
try
{
Invoker.invokeAsync( ATOMIC_MANAGER_SERVER_ALIAS, "get", new Object[] { counterName }, new AtomicCallback( responder ) );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}
public Long getAndIncrement( String counterName )
{
return runGetOperation( "getAndIncrement", counterName );
}
public void getAndIncrement( String counterName, AsyncCallback responder )
{
try
{
Invoker.invokeAsync( ATOMIC_MANAGER_SERVER_ALIAS, "getAndIncrement", new Object[] { counterName }, new AtomicCallback( responder ) );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}
public Long incrementAndGet( String counterName )
{
return runGetOperation( "incrementAndGet", counterName );
}
public void incrementAndGet( String counterName, AsyncCallback responder )
{
try
{
Invoker.invokeAsync( ATOMIC_MANAGER_SERVER_ALIAS, "incrementAndGet", new Object[] { counterName }, new AtomicCallback( responder ) );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}
public Long getAndDecrement( String counterName )
{
return runGetOperation( "getAndDecrement", counterName );
}
public void getAndDecrement( String counterName, AsyncCallback responder )
{
try
{
Invoker.invokeAsync( ATOMIC_MANAGER_SERVER_ALIAS, "getAndDecrement", new Object[] { counterName }, new AtomicCallback( responder ) );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}
public Long decrementAndGet( String counterName )
{
return runGetOperation( "decrementAndGet", counterName );
}
public void decrementAndGet( String counterName, AsyncCallback responder )
{
try
{
Invoker.invokeAsync( ATOMIC_MANAGER_SERVER_ALIAS, "decrementAndGet", new Object[] { counterName }, new AtomicCallback( responder ) );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}
public Long addAndGet( String counterName, Number value )
{
return runGetOperation( "addAndGet", counterName, value );
}
public void addAndGet( String counterName, Number value, AsyncCallback responder )
{
try
{
Invoker.invokeAsync( ATOMIC_MANAGER_SERVER_ALIAS, "addAndGet", new Object[] { counterName, value }, new AtomicCallback( responder ) );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}
public Long getAndAdd( String counterName, Number value )
{
return runGetOperation( "getAndAdd", counterName, value );
}
public void getAndAdd( String counterName, Number value, AsyncCallback responder )
{
try
{
Invoker.invokeAsync( ATOMIC_MANAGER_SERVER_ALIAS, "getAndAdd", new Object[] { counterName, value }, new AtomicCallback( responder ) );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}
public boolean compareAndSet( String counterName, Number expected, Number updated )
{
return Invoker.invokeSync( ATOMIC_MANAGER_SERVER_ALIAS, "compareAndSet", new Object[] { counterName, expected, updated } );
}
public void compareAndSet( String counterName, Number expected, Number updated, AsyncCallback responder )
{
try
{
Invoker.invokeAsync( ATOMIC_MANAGER_SERVER_ALIAS, "compareAndSet", new Object[] { counterName, expected, updated }, responder );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}
public Set list( String pattern )
{
final Object[] objectArray = Invoker.invokeSync( ATOMIC_MANAGER_SERVER_ALIAS, "list", new Object[] { pattern } );
return new HashSet<>( Arrays.asList( Arrays.copyOf( objectArray, objectArray.length, String[].class ) ) );
}
public void list( String pattern, AsyncCallback> responder )
{
try
{
Invoker.invokeAsync( ATOMIC_MANAGER_SERVER_ALIAS, "list", new Object[] { pattern }, responder, ResponderHelper.getSetAdaptingResponder( String.class ) );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}
private Long runGetOperation( String operationName, String counterName )
{
Number responseValue = Invoker.invokeSync( ATOMIC_MANAGER_SERVER_ALIAS, operationName, new Object[] { counterName } );
responseValue = convertToType( responseValue, Long.class );
return (Long) responseValue;
}
private Long runGetOperation( String operationName, String counterName, Number value )
{
Number responseValue = Invoker.invokeSync( ATOMIC_MANAGER_SERVER_ALIAS, operationName, new Object[] { counterName, value } );
responseValue = convertToType( responseValue, Long.class );
return (Long) responseValue;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy