Example usage for java.util Deque pollLast

List of usage examples for java.util Deque pollLast

Introduction

In this page you can find the example usage for java.util Deque pollLast.

Prototype

E pollLast();

Source Link

Document

Retrieves and removes the last element of this deque, or returns null if this deque is empty.

Usage

From source file:Main.java

public static void main(String[] args) {

    Deque<Integer> deque = new ArrayDeque<Integer>(8);

    deque.add(25);//  w  w w .j  av a2s .c o  m
    deque.add(30);
    deque.add(20);
    deque.add(18);

    System.out.println(deque);
    int retval = deque.pollLast();
    System.out.println("Element removed is " + retval);

    System.out.println(deque);
}

From source file:com.qcadoo.model.api.utils.TreeNumberingServiceImpl.java

public static void incrementLastChainNumber(final Deque<String> chain) {
    Integer nextNumber = Integer.valueOf(chain.pollLast()) + 1;
    chain.addLast(nextNumber.toString());
}

From source file:co.cask.cdap.gateway.handlers.metrics.MetricsSuiteTestBase.java

public static void stop() {
    collectionService.stopAndWait();/*from ww w. j  a v  a 2s . c o  m*/

    Deque<File> files = Lists.newLinkedList();
    files.add(dataDir);

    File file = files.peekLast();
    while (file != null) {
        File[] children = file.listFiles();
        if (children == null || children.length == 0) {
            files.pollLast().delete();
        } else {
            Collections.addAll(files, children);
        }
        file = files.peekLast();
    }
}

From source file:org.aksw.simba.bengal.triple2nl.converter.DefaultIRIConverter.java

private static String splitCamelCase(String s) {
    StringBuilder sb = new StringBuilder();
    for (String token : s.split(" ")) {
        String[] split = StringUtils.splitByCharacterTypeCamelCase(token);
        Deque<String> list = new ArrayDeque<>();
        for (int i = 0; i < split.length; i++) {
            String s1 = split[i];
            if (i > 0 && s1.length() == 1 && !org.apache.commons.lang3.StringUtils.isNumeric(s1)) { // single
                // character
                // ->
                // append
                // to
                // previous
                // token
                list.add(list.pollLast() + s1);
            } else {
                list.add(s1);/*from w  ww.j a v a2  s  .  c  o m*/
            }
        }
        sb.append(StringUtils.join(list, ' ')).append(" ");
    }
    return sb.toString().trim();
    // return s.replaceAll(
    // String.format("%s|%s|%s",
    // "(?<=[A-Z])(?=[A-Z][a-z])",
    // "(?<=[^A-Z])(?=[A-Z])",
    // "(?<=[A-Za-z])(?=[^A-Za-z])"
    // ),
    // " "
    // );
}

From source file:com.playtech.portal.platform.portlet.spring.InternalResourceViewResolver.java

protected String viewNameForLicensee(String viewName) {
    String[] pathParts = split(viewName, "/");
    if (pathParts == null)
        return null;

    Deque<String> deque = new LinkedList<>(asList(pathParts));
    String view = deque.pollLast();
    deque.offer(licenseePackageName);//  w ww .ja v a 2s .c om
    deque.offer(view);
    return join(deque, "/");
}

From source file:amfservices.actions.PGServicesAction.java

public Map<String, Object> takeRandomizePrizeAction(String uid, long now) {
    UserTempData uTempData = UserTempData.getTempData(uid);

    int nTurn = PGHelper.toInteger(uTempData.getData(PGMacro.RAND_PRIZE_TURN));
    PGException.Assert(nTurn > 0, PGError.NOT_ENOUGH_RP_TURN, "You have 0 turn");

    // reduce turn
    --nTurn;/*from  www.  j a v a2 s.  c o  m*/
    uTempData.setData(PGMacro.RAND_PRIZE_TURN, nTurn);

    String prizeID = PGConfig.inst().getRandomizePrizes().randomPrize();

    CFRandomizePrize.Prize prizeData = PGConfig.inst().getRandomizePrizes().get(prizeID);
    if (prizeData.isAutoPrize()) {
        PGPrize prize = PrizeFactory.getPrize(prizeData.getPrize());
        EntityContext context = EntityContext.getContext(uid);
        Map<String, Object> pzDesc = prize.award(context, now);
        context.saveToDB();

        // find total gold prized:
        Deque<Map<String, Object>> pzStack = new ArrayDeque();
        int totalGoldPrized = 0;
        pzStack.add(prizeData.getPrize());
        while (!pzStack.isEmpty()) {
            Map<String, Object> pz = pzStack.pollLast();
            for (Map.Entry<String, Object> pzEntry : pz.entrySet()) {
                String pzKey = pzEntry.getKey();
                Object pzVal = pzEntry.getValue();

                if (pzVal instanceof Map) {
                    pzStack.addLast((Map) pzVal);
                } else if ("gold".equals(pzKey)) {
                    totalGoldPrized += PGHelper.toInteger(pzVal);
                }
            }
        }

        if (totalGoldPrized > 0) {
            QuestLogger qLogger = QuestServices.inst().getQuestLogger(uid, now);
            qLogger.log(new GoldDialRecord(totalGoldPrized));
        }

        return AMFBuilder.make(PGMacro.RAND_PRIZE_ID, prizeID, PGMacro.PRIZE, pzDesc);
    } else {
        String giftID = GiftServices.inst().sendGift(Arrays.asList(new String[] { uid }), prizeData.getPrize(),
                now, PGConfig.inst().temp().RandomizePrize_Expire()).getGiftID();

        return AMFBuilder.make(PGMacro.RAND_PRIZE_ID, prizeID, PGMacro.GIFT_ID, giftID);
    }
}

From source file:de.themoep.simpleteampvp.games.SimpleTeamPvPGame.java

/**
 * Balance the teams/*from  w w w.ja v a  2  s .  c  o m*/
 * @return <tt>true</tt> if game is in GameState.JOINING and players can be balanced
 */
public boolean balance() {
    if (getState() != GameState.JOINING)
        return false;

    plugin.getServer()
            .broadcastMessage(ChatColor.GREEN + "Ausbalancieren und Auffllen der Teams gestartet...");

    Map<Player, String> beforeBalance = new HashMap<>();
    List<Player> playersToJoin = new ArrayList<>();
    for (Player player : plugin.getServer().getOnlinePlayers()) {
        if (player.hasPermission(SimpleTeamPvP.BYPASS_PERM) || player.getGameMode() == GameMode.CREATIVE
                || player.getGameMode() == GameMode.SPECTATOR)
            continue;
        TeamInfo team = getTeam(player);
        if (team == null) {
            if (config.getRandomRegion() == null || config.getRandomRegion().contains(player.getLocation()))
                playersToJoin.add(player);
            beforeBalance.put(player, "");
        } else {
            beforeBalance.put(player, team.getName());
        }
    }
    plugin.getLogger().log(Level.INFO, "Players to join: " + playersToJoin.size());

    int totalPlayers = playersToJoin.size();
    for (TeamInfo team : config.getTeams().values()) {
        totalPlayers += team.getSize();
    }
    plugin.getLogger().log(Level.INFO, "Number of teams: " + config.getTeams().size());
    double perfectSize = (double) totalPlayers / (double) config.getTeams().size();

    plugin.getLogger().log(Level.INFO, "perfectSize: " + perfectSize);

    if (plugin.getServerTags() != null) {
        // Team key -> Tag
        Map<String, String> teamTags = new HashMap<>();

        for (TeamInfo team : config.getTeams().values()) {

            Map<String, Integer> tags = new HashMap<>();
            for (String playerName : team.getScoreboardTeam().getEntries()) {
                Player player = plugin.getServer().getPlayer(playerName);
                if (player == null)
                    continue;

                String tag = "no server";
                ServerInfo serverInfo = plugin.getServerTags().getPlayerServer(player);
                if (serverInfo != null) {
                    tag = serverInfo.getTag();
                }
                if (!tags.containsKey(tag)) {
                    tags.put(tag, 0);
                }
                tags.put(tag, tags.get(tag) + 1);
            }

            String teamTag = "no server";
            int tagCount = 0;
            for (Map.Entry<String, Integer> entry : tags.entrySet()) {
                if (entry.getValue() > tagCount) {
                    tagCount = entry.getValue();
                    teamTag = entry.getKey();
                }
            }

            teamTags.put(team.getName(), teamTag);
        }

        for (TeamInfo team : config.getTeams().values()) {
            // Filter out players that come from another server than the majority of the team
            // and remove them as long as the team is larger than the perfect size
            for (String playerName : team.getScoreboardTeam().getEntries()) {
                if (team.getSize() <= perfectSize + 0.5)
                    break;

                Player player = plugin.getServer().getPlayer(playerName);
                if (player == null)
                    continue;

                String tag = "no server";
                ServerInfo serverInfo = plugin.getServerTags().getPlayerServer(player);
                if (serverInfo != null) {
                    tag = serverInfo.getTag();
                }

                if (tag.equals(teamTags.get(team.getName())))
                    continue;

                plugin.getLogger().log(Level.INFO,
                        "[ST] Removed " + player.getName() + " from " + team.getName() + " (Step 1)");

                team.removePlayer(player);
                playersToJoin.add(player);
            }

            // Team still larger than the perfect size? Remove last joined player
            Deque<String> teamMates = new ArrayDeque<>(team.getScoreboardTeam().getEntries());
            while (team.getSize() > perfectSize + 0.5) {
                String name = teamMates.peekLast();
                Player player = plugin.getServer().getPlayer(name);
                if (player == null)
                    continue;

                team.removePlayer(player);
                plugin.getLogger().log(Level.INFO,
                        "[ST] Removed " + player.getName() + " from " + team.getName() + " (Step 2)");
                teamMates.pollLast();
                playersToJoin.add(player);
            }
        }

        // Add rest of players to teams from their server
        Iterator<Player> playerIterator = playersToJoin.iterator();
        while (playerIterator.hasNext()) {
            Player player = playerIterator.next();
            ServerInfo serverInfo = plugin.getServerTags().getPlayerServer(player);
            if (serverInfo != null && teamTags.containsValue(serverInfo.getTag())) {
                for (TeamInfo team : config.getTeams().values()) {
                    if (team.getSize() < perfectSize - 0.5 && teamTags.containsKey(team.getName())
                            && teamTags.get(team.getName()).equals(serverInfo.getTag())) {
                        team.addPlayer(player);
                        plugin.getLogger().log(Level.INFO,
                                "[ST] Added " + player.getName() + " to " + team.getName());
                        playerIterator.remove();
                        break;
                    }
                }
            }
        }
        plugin.getLogger().log(Level.INFO, "Players to join after servertags: " + playersToJoin.size());
    }

    // Remove players from teams that have more than the perfect size
    for (TeamInfo team : config.getTeams().values()) {
        for (String playerName : team.getScoreboardTeam().getEntries()) {
            if (team.getSize() <= perfectSize + 0.5)
                break;

            Player player = plugin.getServer().getPlayer(playerName);
            if (player == null)
                continue;

            plugin.getLogger().log(Level.INFO, "Removed " + player.getName() + " from " + team.getName());

            team.removePlayer(player);
            playersToJoin.add(player);
        }
    }

    Iterator<Player> playerIterator = playersToJoin.iterator();
    for (TeamInfo team : config.getTeams().values()) {
        while (playerIterator.hasNext()) {
            if (team.getSize() >= perfectSize - 0.5)
                break;

            Player player = playerIterator.next();
            team.addPlayer(player);
            plugin.getLogger().log(Level.INFO, "Added " + player.getName() + " to " + team.getName());
            playerIterator.remove();
        }
    }

    if (playerIterator.hasNext()) {
        plugin.getLogger().log(Level.INFO, "Adding " + playersToJoin.size()
                + " remaining players to teams according to their player count:");

        List<TeamInfo> teams = new ArrayList<>(config.getTeams().values());
        teams.sort((t1, t2) -> Integer.compare(t2.getSize(), t1.getSize()));

        for (TeamInfo team : teams) {
            while (playerIterator.hasNext()) {
                if (team.getSize() > perfectSize)
                    break;

                Player player = playerIterator.next();
                team.addPlayer(player);
                plugin.getLogger().log(Level.INFO,
                        "Added remaining player " + player.getName() + " to " + team.getName());
                playerIterator.remove();
            }
        }
    }

    if (playerIterator.hasNext()) {
        plugin.getLogger().log(Level.INFO,
                "Adding " + playersToJoin.size() + " remaining players to totally random teams:");
        Random r = new Random();
        List<TeamInfo> teams = new ArrayList<>(config.getTeams().values());
        while (playerIterator.hasNext()) {
            Player player = playerIterator.next();
            TeamInfo team = teams.get(r.nextInt(teams.size()));
            team.addPlayer(player);
            plugin.getLogger().log(Level.INFO,
                    "Added player " + player.getName() + " to " + team.getName() + " by random");
            playerIterator.remove();
        }
    }
    plugin.getLogger().log(Level.INFO, "All players joined! (" + playersToJoin.size() + ")");

    for (Map.Entry<Player, String> entry : beforeBalance.entrySet()) {
        TeamInfo team = getTeam(entry.getKey());
        if (team != null && !team.getName().equals(entry.getValue())) {
            Player player = null;
            for (Iterator<String> it = team.getScoreboardTeam().getEntries().iterator(); player == null
                    && it.hasNext();) {
                player = plugin.getServer().getPlayer(it.next());
            }
            if (player != null && team.getJoinRegion().contains(player.getLocation())) {
                entry.getKey().teleport(player);
            } else {
                entry.getKey().teleport(team.getJoinRegion().calculateMiddle().getLocation());
            }
        }
    }

    plugin.getServer().broadcastMessage(ChatColor.GREEN + "Teams ausbalanciert und aufgefllt!");

    state = GameState.WAITING;
    return true;
}