Wednesday, November 6, 2013

How to create a bit state register java android

Hi there! Today i'gonna share/show how we could create a bit state register, manupulate bit states doing OR and AND operations on it. Favor/consider always the state pattern whenever possible over this solution here. I use it when the state pattern is not our first choice or is inappropriate. (when it gets to complex or i'm doing simple state checks and the design is simple, fixed and not intented to be extended) It is a simple 32 states bit processor which can hold up to max 32 states. The result at the end will be something like in the class diagram bellow:



BitStates

/**
 * Bit states to represent 32 possible states. The first state is the mask state to reset states.
 * Those states can be used to define component transition states while animating it.
 * 
INSTRUCTIONS ON HOW TO USE IT:

 * In combination with a state pattern, you may associate any bit state to a transition, position, animation or whatever you need to monitor in a proper sequence. 
 * For example: If you want to know that a button X has been pressed, animation Y is done and Fragment Z is shown, associate a bit state to each of them
 * and do an "or-operation" on it. You'll be able now to extract all those information (3 states) from just one bit combination without having to explicitly ask for it using a lot of if/else cases.
 * 
  BIT STATE REPRESENTATION            HEX CODE REPRESENTATION
  00000000000000000000000000000000 0
 00000000000000000000000000000001 1
 00000000000000000000000000000010 2
 00000000000000000000000000000100 4
 00000000000000000000000000001000 8
 00000000000000000000000000010000 10
 00000000000000000000000000100000 20
 00000000000000000000000001000000 40
 00000000000000000000000010000000 80
 00000000000000000000000100000000 100
 00000000000000000000001000000000 200
 00000000000000000000010000000000 400
 00000000000000000000100000000000 800
 00000000000000000001000000000000 1000
 00000000000000000010000000000000 2000
 00000000000000000100000000000000 4000
 00000000000000001000000000000000 8000
 00000000000000010000000000000000 10000
 00000000000000100000000000000000 20000
 00000000000001000000000000000000 40000
 00000000000010000000000000000000 80000
 00000000000100000000000000000000 100000
 00000000001000000000000000000000 200000
 00000000010000000000000000000000 400000
 00000000100000000000000000000000 800000
 00000001000000000000000000000000 1000000
 00000010000000000000000000000000 2000000
 00000100000000000000000000000000 4000000
 00001000000000000000000000000000 8000000
 00010000000000000000000000000000 10000000
 00100000000000000000000000000000 20000000
 01000000000000000000000000000000 40000000
 10000000000000000000000000000000 80000000
 * 
* @author Ricardo Ferreira 01/11/2013 * */ public enum BitStates { STATE_00(0x0), STATE_01(0x1), STATE_02(0x2), STATE_03(0x4), STATE_04(0x8), STATE_05(0x10), STATE_06(0x20), STATE_07(0x40), STATE_08(0x80), STATE_09(0x100), STATE_10(0x200), STATE_11(0x400), STATE_12(0x800), STATE_13(0x1000), STATE_14(0x2000), STATE_15(0x4000), STATE_16(0x8000), STATE_17(0x10000), STATE_18(0x20000), STATE_19(0x40000), STATE_20(0x80000), STATE_21(0x100000), STATE_22(0x200000), STATE_23(0x400000), STATE_24(0x800000), STATE_25(0x1000000), STATE_26(0x2000000), STATE_27(0x4000000), STATE_28(0x8000000), STATE_29(0x10000000), STATE_30(0x20000000), STATE_31(0x40000000), STATE_32(0x80000000); private int state; private BitStates(int state) { this.state = state; } public int getState() { return state; } }

BitStateRegister

import java.util.HashMap;
import java.util.Map;

// Singleton Pattern
/**
 * Use this register to associate bit states to concrete states in a state pattern.
 * This register can hold up to 33 bit states,while the 0 state is per default the
 * zero state (reset mask state).
 * @author Ricardo Ferreira 02/11/2013
 *
 */
public class BitStateRegister {

 private static final BitStateRegister INSTANCE = new BitStateRegister();
 private static final Map stateRegister = new HashMap();
 private static final int registerMaxSize = 33;

 private BitStateRegister() {
  // Singleton Pattern
 }

 public static BitStateRegister getInstance() {
  registerZeroStateOnlyOnce();
  return INSTANCE;
 }
 
 private static void registerZeroStateOnlyOnce(){
  if (!stateRegister.containsKey("zero")) {
   stateRegister.put("zero", BitStates.STATE_00.getState());
  }
 }

 /**
  * State Register. It registers a new state only if not exists already. 

  * This Register is able to register from 1 up to 32 states in a 32 bit system.

  * You do not have to care about it. The states will be created and increased

  * automatically, when registered.

 
  * 
  * To obtain the zero state(reset mask state), call getState("zero"); on it.

  * To check if the register is already completely full, call isRegisterComplete(); on it.
  * 
  * @param stateClassSimpleName the simple class name provided by calling: ConcreteState.class.getSimpleName(); on your concrete state.
  * @return true if the new state was created, false if the state already exists or the register is full.
  */
 public boolean registerStateFor(String stateClassSimpleName) {
  boolean operationResult = false;
  if (!stateRegister.containsKey(stateClassSimpleName) && stateRegister.size() < registerMaxSize) {
   int nextStateIndex = stateRegister.size();
   int newState = BitStates.values()[nextStateIndex].getState();
   stateRegister.put(stateClassSimpleName, newState);
   operationResult = true;
  }
  return operationResult;
 }

 public boolean unregisterStateFor(String stateClassSimpleName){
  boolean operationResult = false;
  if (stateRegister.containsKey(stateClassSimpleName)) {
   Integer bitState = stateRegister.get(stateClassSimpleName);
   stateRegister.remove(bitState);
   operationResult = true;
  }
  return operationResult;
 }

 public int getStateFor(String stateClassSimpleName) {
  return stateRegister.get(stateClassSimpleName);
 }

 /**
  * Returns true if the Register is already completely filled up with 32 states , false otherwise.
  * 
  * @return true if the Register is already completely filled up with 32 states , false otherwise
  */
 public boolean isRegisterComplete() {
  return stateRegister.size() == registerMaxSize;
 }
 
 /**
  * Checks if the state of the given state class name is set in the current bit state.
  * @param currentBitState the current bit state
  * @param stateClassSimpleName the simple class name provided by calling: ConcreteState.class.getSimpleName(); on your concrete state.
  * @return true if the bit is set, false otherwise.
  */
 public boolean isBitStateSetted(int currentBitState, String stateClassSimpleName){
  boolean operationResult = false;
  if(stateRegister.containsKey(stateClassSimpleName)){
   int stateFor = getStateFor(stateClassSimpleName);
   if((stateFor&currentBitState)==stateFor){
    operationResult = true;
   }
  }
  return operationResult;
 }

}

BitStateProcessor

/**
 * A bit state processor with a process state capacity of max 32 states.
 * Favor/consider always the usage of a state pattern instead of it. 
 * But in case the state pattern is inappropriate, you can use it.
 * 
 * @author Ricardo Ferreira 06/11/2013
 * 
 */
public class BitStateProcessor {
 private int resetMask = BitStateRegister.getInstance().getStateFor("zero");
 private int currentBitState = 0x0;
 private int internalState = 0x0;
 private String stateClassSimpleName;

 /**
  * @param stateClassSimpleName
  *            the simple class name of your concrete state provided by calling ConcreteState.class.getSimpleName();
  */
 public BitStateProcessor(int currentBitState, String stateClassSimpleName) {
  this.currentBitState = currentBitState;
  this.stateClassSimpleName = stateClassSimpleName;
  BitStateRegister.getInstance().registerStateFor(stateClassSimpleName);
  this.internalState = BitStateRegister.getInstance().getStateFor(stateClassSimpleName);
 }

 public int getCurrentBitState() {
  return this.currentBitState;
 }

 public void setCurrentBitState(int currentBitState) {
  this.currentBitState = currentBitState;
 }

 public void resetCurrentBitState() {
  this.currentBitState = this.currentBitState & this.resetMask;
 }

 public boolean isBitStateSetted() {
  return BitStateRegister.getInstance().isBitStateSetted(this.currentBitState, this.stateClassSimpleName);
 }

 public boolean unregisterStateFor(String stateClassSimpleName) {
  return BitStateRegister.getInstance().unregisterStateFor(stateClassSimpleName);
 }

 public boolean isRegisterComplete() {
  return BitStateRegister.getInstance().isRegisterComplete();
 }

 public int getStateFor(String stateClassSimpleName) {
  return BitStateRegister.getInstance().getStateFor(stateClassSimpleName);
 }

 /**
  * Returns a new bit state.
  * 
  * @param currentBitState the actual bit state passed to this class or method
  * @return the new bit state after an "or-operarion" with its internal state.
  */
 public int processCurrentBitState(int currentBitState) {
  this.currentBitState = this.internalState | currentBitState;
  return this.currentBitState;
 }

}

That's all! hope you like it!

😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘‡

Be sure to read, it will change your life!
Show your work by Austin Kleonhttps://amzn.to/34NVmwx

This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practiceshttps://amzn.to/30WQSm2

Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv

This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp

Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy

😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘†

Monday, November 4, 2013

How to lock state to synchonize transitions java android

Hi there!
Today i'm gonna share one of many possible ways to acquire a lock with the intent of synchronize transitions or whatever task you may have. I use it to synchronize complex animation transition sequences in Android.


import java.util.HashSet;
import java.util.Set;

/**
 * Use this class to synchronize transitions, animations or whatever you want.

 * It is designed to acquire and release a synchronized lock state, coordinating

 * the access of it in a multi thread environment.
 * 
 * @author Ricardo Ferreira 04/11/2013
 */
public class StateLock {

 private final static StateLock INSTANCE = new StateLock();
 private Set lockSet = new HashSet();
 private boolean lock = false;
 
 private StateLock() {
  // singleton
 }

 /** Synchronized because the lock state should be acquired and released in a controlled way */
 public synchronized static StateLock getInstance() {
  return StateLock.INSTANCE;
 }

 public synchronized boolean isStateLocked() {
  return lock;
 }
 
 public synchronized boolean hasStateAlreadyLocked(String stateSimpleClassName) {
  return lockSet.contains(stateSimpleClassName);
 }

 /** Returns true if the state lock was acquired by the given class, false otherwise */
 public synchronized boolean acquireLockState(String stateSimpleClassName) {
  boolean operationResult = false;
  if (lockSet.isEmpty()) {
   lockSet.add(stateSimpleClassName);
   lock = true;
   operationResult = true;
  }
  return operationResult;
 }

 /** Returns true if the lock was released or no state was locked, false otherwise */
 public synchronized boolean releaseLockState(String stateSimpleClassName) {
  boolean operationResult = false;
  if (lockSet.contains(stateSimpleClassName)) {
   lockSet.clear();
   lock = false;
   operationResult = true;
  } else if (lockSet.isEmpty()) {
   lock = false;
   operationResult = true;
  }
  return operationResult;
 }

 protected Object clone() throws CloneNotSupportedException {
  throw new CloneNotSupportedException();
 }

}

😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘‡

Be sure to read, it will change your life!
Show your work by Austin Kleonhttps://amzn.to/34NVmwx

This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practiceshttps://amzn.to/30WQSm2

Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv

This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp

Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy

😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘†