All Downloads are FREE. Search and download functionalities are using the official Maven repository.

brainslug.flow.execution.impl.HashMapTokenStore Maven / Gradle / Ivy

There is a newer version: 0.21
Show newest version
package brainslug.flow.execution.impl;

import brainslug.flow.context.IdGenerator;
import brainslug.flow.execution.Token;
import brainslug.flow.execution.TokenList;
import brainslug.flow.execution.TokenStore;
import brainslug.flow.model.Identifier;
import brainslug.util.Option;

import java.util.*;

public class HashMapTokenStore implements TokenStore {

  Map> flowToInstanceMap = Collections.synchronizedMap(new HashMap>());
  Map> instanceToTokenMap = new HashMap>();

  IdGenerator idGenerator;

  public HashMapTokenStore(IdGenerator idGenerator) {
    this.idGenerator = idGenerator;
  }

  @Override
  public TokenList getInstanceTokens(Identifier instanceId) {
    if (instanceToTokenMap.get(instanceId) == null) {
      List instanceTokens = new ArrayList();
      instanceToTokenMap.put(instanceId, instanceTokens);
      return new TokenList(instanceTokens);
    } else {
      return new TokenList(instanceToTokenMap.get(instanceId));
    }
  }

  @Override
  public TokenList getNodeTokens(Identifier nodeId, Identifier instanceId) {
    List nodeTokens = new ArrayList();
    for (final Iterator instanceTokens = getInstanceTokens(instanceId).getIterator(); instanceTokens.hasNext(); ) {
      Token token = instanceTokens.next();
      if(token.getNodeId().equals(nodeId)) {
        nodeTokens.add(token);
      }
    }
    return new TokenList(nodeTokens);
  }

  @Override
  public Token addToken(Identifier instanceId, Identifier nodeId, Option sourceNodeId) {
    Token token = new Token(idGenerator.generateId(),
      nodeId, sourceNodeId,
      Option.of(instanceId), false);

    getInstanceTokens(instanceId).add(token);
    return token;
  }

  @Override
  public void removeToken(Identifier instanceId, Identifier tokenIdToDelete) {
    for (final Iterator instanceTokens = getInstanceTokens(instanceId).getIterator(); instanceTokens.hasNext(); ) {
      Token nextToken = instanceTokens.next();
      if (nextToken.getId().equals(tokenIdToDelete)) {
        instanceTokens.remove();
      }
    }
  }

  @Override
  public Identifier createInstance(Identifier definitionId) {
    Identifier instanceId = idGenerator.generateId();
    getOrCreateInstanceList(definitionId).add(instanceId);
    return instanceId;
  }

  protected List getOrCreateInstanceList(Identifier flowId) {
    if (flowToInstanceMap.get(flowId) == null) {
      flowToInstanceMap.put(flowId, new ArrayList());
    }
    return flowToInstanceMap.get(flowId);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy