Example usage for java.util Collection toString

List of usage examples for java.util Collection toString

Introduction

In this page you can find the example usage for java.util Collection toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:de.schildbach.game.chess.ChessRules.java

@Override
protected String[] basicFormatGameArray(Game game, Locale locale) {
    int size = game.getSize();
    String[] array = new String[size];

    ChessBoardGeometry geometry = (ChessBoardGeometry) getBoardGeometry();
    ChessLikePosition position = (ChessLikePosition) game.getInitialPosition().clone();
    Board board = position.getBoard();//from   w  w  w  . j  a v a2  s  .c o m
    Board initialBoard = game.getInitialPosition().getBoard();
    PieceSet pieceSet = getPieceSet();

    StringBuilder notation = new StringBuilder();
    for (int index = 0; index < size; index++) {
        ChessMove move = (ChessMove) game.getMove(index);
        Coordinate source = move.getSource();
        Coordinate target = move.getTarget();
        Piece piece = board.getPiece(source);
        String sourceNotation = source.getNotation();
        char sourceFile = sourceNotation.charAt(0);
        char sourceRank = sourceNotation.charAt(1);

        notation.setLength(0);

        if (looksLikeCastlingMove(move, board)) {
            if (geometry.isKingSide(source, target))
                notation.append("O-O");
            else
                notation.append("O-O-O");
        } else {
            Collection<? extends ChessLikeMove> potentialMoves = allowedMoves(position, initialBoard);

            // narrow by target
            for (Iterator<? extends ChessLikeMove> iAllowed = potentialMoves.iterator(); iAllowed.hasNext();) {
                if (!iAllowed.next().getTarget().equals(target))
                    iAllowed.remove();
            }
            notation.append(target.getNotation());

            boolean pawn = piece instanceof Pawn || piece instanceof BerolinaPawn;

            // narrow by capture
            boolean capture = looksLikePlainCapture(move, board) || looksLikeEnPassantCapture(move, board);
            if (capture) {
                for (Iterator<? extends ChessLikeMove> iAllowed = potentialMoves.iterator(); iAllowed
                        .hasNext();) {
                    if (!looksLikeCapture(iAllowed.next(), board))
                        iAllowed.remove();
                }
                notation.insert(0, 'x');

                if (pawn) {
                    // narrow to source file
                    for (Iterator<? extends ChessLikeMove> iAllowed = potentialMoves.iterator(); iAllowed
                            .hasNext();) {
                        if (!geometry.sameFile(iAllowed.next().getSource(), source))
                            iAllowed.remove();
                    }
                    notation.insert(0, sourceFile);
                }
            }

            // narrow by piece
            for (Iterator<? extends ChessLikeMove> iAllowed = potentialMoves.iterator(); iAllowed.hasNext();) {
                if (!board.getPiece(iAllowed.next().getSource()).equals(piece))
                    iAllowed.remove();
            }

            // narrow by promotion
            for (Iterator<? extends ChessLikeMove> iAllowed = potentialMoves.iterator(); iAllowed.hasNext();) {
                if (!ObjectUtils.equals(((ChessMove) iAllowed.next()).getPromotionPiece(),
                        move.getPromotionPiece()))
                    iAllowed.remove();
            }
            if (move.getPromotionPiece() != null)
                notation.append("=" + pieceSet.getCharRepresentation(move.getPromotionPiece(), locale));

            if (potentialMoves.size() > 1) {
                boolean sameRank = false;
                boolean sameFile = false;
                for (ChessLikeMove potentialMove : potentialMoves) {
                    Coordinate potentialSource = potentialMove.getSource();
                    if (!potentialSource.equals(source)) {
                        if (geometry.sameFile(potentialSource, source))
                            sameFile = true;
                        if (geometry.sameRank(potentialSource, source))
                            sameRank = true;
                    }
                }
                if (sameRank && sameFile) {
                    // narrow to source
                    for (Iterator<? extends ChessLikeMove> iAllowed = potentialMoves.iterator(); iAllowed
                            .hasNext();) {
                        if (!iAllowed.next().getSource().equals(source))
                            iAllowed.remove();
                    }
                    notation.insert(0, sourceNotation);
                } else if (!sameFile) {
                    // narrow to source file
                    for (Iterator<? extends ChessLikeMove> iAllowed = potentialMoves.iterator(); iAllowed
                            .hasNext();) {
                        if (!geometry.sameFile(iAllowed.next().getSource(), source))
                            iAllowed.remove();
                    }
                    notation.insert(0, sourceFile);
                } else {
                    // narrow to source rank
                    for (Iterator<? extends ChessLikeMove> iAllowed = potentialMoves.iterator(); iAllowed
                            .hasNext();) {
                        if (!geometry.sameRank(iAllowed.next().getSource(), source))
                            iAllowed.remove();
                    }
                    notation.insert(0, sourceRank);
                }
            }

            // piece character
            if (!pawn)
                notation.insert(0, pieceSet.getCharRepresentation(piece.getClass(), locale));

            // sanity check
            if (potentialMoves.size() != 1)
                throw new IllegalStateException(potentialMoves.toString());
        }

        // execute move
        List<MicroOperation> ops = disassembleMove(move, position, game.getInitialPosition().getBoard());
        doOperations(ops, position);

        // check
        CheckState checkState = checkState(position, game.getInitialPosition().getBoard());
        if (checkState == CheckState.CHECKMATE) {
            notation.append("#");
        } else if (checkState == CheckState.CHECK) {
            notation.append("+");
        }

        array[index] = notation.toString();
    }

    return array;
}

From source file:org.opencms.setup.CmsSetupBean.java

/**
 * Initializes and validates the read components.<p>
 * /*from  w w w . j  av a 2s. c om*/
 * @param modules a modifiable list of the modules to be imported
 */
protected void initializeComponents(Collection<String> modules) {

    Iterator<CmsSetupComponent> itGroups = new ArrayList<CmsSetupComponent>(
            CmsCollectionsGenericWrapper.<CmsSetupComponent>list(m_components.elementList())).iterator();
    while (itGroups.hasNext()) {
        CmsSetupComponent component = itGroups.next();
        String errMsg = "";
        String warnMsg = "";
        // check name
        if (CmsStringUtil.isEmptyOrWhitespaceOnly(component.getName())) {
            errMsg += Messages.get().container(Messages.ERR_COMPONENT_NAME_EMPTY_1, component.getId()).key();
            errMsg += "\n";
        }
        // check description
        if (CmsStringUtil.isEmptyOrWhitespaceOnly(component.getName())) {
            warnMsg += Messages.get().container(Messages.LOG_WARN_COMPONENT_DESC_EMPTY_1, component.getId())
                    .key();
            warnMsg += "\n";
        }
        // check position
        if (component.getPosition() == DEFAULT_POSITION) {
            warnMsg += Messages.get().container(Messages.LOG_WARN_COMPONENT_POS_EMPTY_1, component.getId())
                    .key();
            warnMsg += "\n";
        }
        // check dependencies
        Iterator<String> itDeps = component.getDependencies().iterator();
        while (itDeps.hasNext()) {
            String dependency = itDeps.next();
            if (m_components.getObject(dependency) == null) {
                errMsg += Messages.get().container(Messages.LOG_WARN_COMPONENT_DEPENDENCY_BROKEN_2,
                        component.getId(), dependency).key();
                errMsg += "\n";
            }
        }
        // check modules match
        boolean match = false;
        Iterator<String> itModules = modules.iterator();
        while (itModules.hasNext()) {
            String module = itModules.next();
            if (component.match(module)) {
                match = true;
                itModules.remove();
            }
        }
        if (!match) {
            errMsg += Messages.get().container(Messages.ERR_COMPONENT_MODULES_EMPTY_1, component.getId()).key();
            errMsg += "\n";
        }

        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(errMsg)) {
            m_components.removeObject(component.getId());
            if (LOG.isErrorEnabled()) {
                LOG.error(errMsg);
            }
        }
        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(warnMsg)) {
            if (LOG.isWarnEnabled()) {
                LOG.warn(warnMsg);
            }
        }
    }
    if (!modules.isEmpty()) {
        if (LOG.isWarnEnabled()) {
            LOG.warn(Messages.get().container(Messages.LOG_WARN_MODULES_LEFT_1, modules.toString()));
        }
    }
}