/** * Retrieves the state of an object for binding. * * <p>Service providers that implement the <tt>DirContext</tt> interface should use * <tt>DirectoryManager.getStateToBind()</tt>, not this method. Service providers that implement * only the <tt>Context</tt> interface should use this method. * * <p>This method uses the specified state factories in the <tt>Context.STATE_FACTORIES</tt> * property from the environment properties, and from the provider resource file associated with * <tt>nameCtx</tt>, in that order. The value of this property is a colon-separated list of * factory class names that are tried in order, and the first one that succeeds in returning the * object's state is the one used. If no object's state can be retrieved in this way, return the * object itself. If an exception is encountered while retrieving the state, the exception is * passed up to the caller. * * <p>Note that a state factory (an object that implements the StateFactory interface) must be * public and must have a public constructor that accepts no arguments. * * <p>The <code>name</code> and <code>nameCtx</code> parameters may optionally be used to specify * the name of the object being created. See the description of "Name and Context Parameters" in * {@link ObjectFactory#getObjectInstance ObjectFactory.getObjectInstance()} for details. * * <p>This method may return a <tt>Referenceable</tt> object. The service provider obtaining this * object may choose to store it directly, or to extract its reference (using * <tt>Referenceable.getReference()</tt>) and store that instead. * * @param obj The non-null object for which to get state to bind. * @param name The name of this object relative to <code>nameCtx</code>, or null if no name is * specified. * @param nameCtx The context relative to which the <code>name</code> parameter is specified, or * null if <code>name</code> is relative to the default initial context. * @param environment The possibly null environment to be used in the creation of the state * factory and the object's state. * @return The non-null object representing <tt>obj</tt>'s state for binding. It could be the * object (<tt>obj</tt>) itself. * @exception NamingException If one of the factories accessed throws an exception, or if an error * was encountered while loading and instantiating the factory and object classes. A factory * should only throw an exception if it does not want other factories to be used in an attempt * to create an object. See <tt>StateFactory.getStateToBind()</tt>. * @see StateFactory * @see StateFactory#getStateToBind * @see DirectoryManager#getStateToBind * @since 1.3 */ public static Object getStateToBind( Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws NamingException { FactoryEnumeration factories = ResourceManager.getFactories(Context.STATE_FACTORIES, environment, nameCtx); if (factories == null) { return obj; } // Try each factory until one succeeds StateFactory factory; Object answer = null; while (answer == null && factories.hasMore()) { factory = (StateFactory) factories.next(); answer = factory.getStateToBind(obj, name, nameCtx, environment); } return (answer != null) ? answer : obj; }
@Override public IMonningState doAction() { if (checkForCollisions()) { Log.w("averagre ", "Walking"); return StateFactory.getInstance().getWalkingState(monning); } return this; /* ArrayList<Point> line = BoundaryResolver.getGroundLine(getSprite()); if(getLandscape().isAir(line)){ //Log.w("FallingState","IS AIr" + line); getBody().setLinearVelocity(0, 3); return this; } else { // Log.w("FallingState","not Air"); getBody().setLinearVelocity(0, 0); return StateFactory.getWalkingState(monning); } */ }
/** Class that handles every new node created (flyweight pattern) */ public class StateClient { private int score; public int getScore() { return score; } public void setScore(int score) { this.score = score; } private Player player; public Player getPlayer() { if (player == null) player = new Player(); return player; } public void setPlayer(Player player) { this.player = player; } private Player nextPlayer; public Player getNextPlayer() { return nextPlayer; } public void setNextPlayer(Player nextPlayer) { this.nextPlayer = nextPlayer; } private String hotel; public String getHotel() { return hotel; } public void setHotel(String hotel) { this.hotel = hotel; } private List<String> shares; public List<String> getShares() { if (shares == null) shares = new ArrayList<>(); return shares; } public void setShares(List<String> shares) { this.shares = shares; } /** Path to this state */ private List<String> path; public List<String> getPath() { if (path == null) path = new ArrayList<>(); return path; } public void setPath(List<String> path) { this.path = path; } /** List of all children */ private List<StateClient> children; public List<StateClient> getChildren() { if (children == null) children = new ArrayList<>(); return children; } public void setChildren(List<StateClient> children) { this.children = children; } private Tile tile; public Tile getTile() { return tile; } public void setTile(Tile tile) { this.tile = tile; } State state = StateFactory.getInstance(); String move; /** * Inspect tile and set type of move in the node. * * @param board * @param row * @param column * @return */ public String inspect(Board board, String row, String column) { ArrayList<String> hotels; hotels = new ArrayList<>(); List<String> neighbors = checkNeighbor(row, column, board); for (String n : neighbors) { if (!hotels.contains(board.getBoard().get(n).getLabel())) hotels.add(board.getBoard().get(n).getLabel()); } if (neighbors.size() > 1) { Set<String> set = new HashSet<>(hotels); // for (String h : hotels) { if (set.size() == 1) { if (hotels.get(0).equals(Config.Moves.SINGLETON.getMove())) { return Config.Moves.FOUNDING.getMove(); } else return Config.Moves.GROWING.getMove(); } else return Config.Moves.MERGING.getMove(); // } } else if (neighbors.size() == 1 && hotels.get(0).equals(Config.Moves.SINGLETON.getMove())) { return Config.Moves.FOUNDING.getMove(); } else if (neighbors.size() == 1 && !hotels.isEmpty()) { return Config.Moves.GROWING.getMove(); } else if (neighbors.size() == 0) { return Config.Moves.SINGLETON.getMove(); } return null; } /** * Method to get neighbors for a tile * * @param r * @param prevCol * @param nextCol * @param row * @param board * @return List<String> consisting of the neighboring tiles */ private List<String> checkNeighbor(String row, String column, Board board) { List<String> positions = new ArrayList<>(); List<String> position = new ArrayList<>(); int rowIndex = board.getRows().indexOf(row); int columnIndex = board.getColumns().indexOf(column); if (rowIndex - 1 > 0) { position.add(column + board.getRows().get(rowIndex - 1)); if (board.getBoard().get(column + board.getRows().get(rowIndex - 1)) != null) { positions.add(column + board.getRows().get(rowIndex - 1)); } } if (rowIndex + 1 < board.getRows().size()) { position.add(column + board.getRows().get(rowIndex + 1)); if (board.getBoard().get(column + board.getRows().get(rowIndex + 1)) != null) { positions.add(column + board.getRows().get(rowIndex + 1)); } } if (columnIndex - 1 > 0) { position.add(board.getColumns().get(columnIndex - 1) + row); if (board.getBoard().get(board.getColumns().get(columnIndex - 1) + row) != null) { positions.add(board.getColumns().get(columnIndex - 1) + row); } } if (columnIndex + 1 < board.getColumns().size()) { position.add(board.getColumns().get(columnIndex + 1) + row); if (board.getBoard().get(board.getColumns().get(columnIndex + 1) + row) != null) { positions.add(board.getColumns().get(columnIndex + 1) + row); } } return positions; } public Map<String, Object> generate(Board board, String row, String column) { // state.generate(board, row, column, inspect (board, row, column)); Map<String, Object> result = new HashMap<String, Object>(); String move = inspect(board, row, column); result.put("move", move); Tile tile = new Tile(); tile.setRow(row); tile.setColumn(column); result.put("tile", tile); return result; } public String getMove() { return move; } public void setMove(String move) { this.move = move; } public State getState() { return state; } public void setState(State state) { this.state = state; } }
private static final State getState(long n) { return StateFactory.lookup((int) n); }