de.tor.tribes.util.bb.VillageListFormatter.java Source code

Java tutorial

Introduction

Here is the source code for de.tor.tribes.util.bb.VillageListFormatter.java

Source

/* 
 * Copyright 2015 Torridity.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.tor.tribes.util.bb;

import de.tor.tribes.types.ext.Village;
import java.text.NumberFormat;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author Torridity
 */
public class VillageListFormatter extends BasicFormatter<Village> {

    private final String[] VARIABLES = new String[] { LIST_START, LIST_END, ELEMENT_COUNT, ELEMENT_ID };
    private final String STANDARD_TEMPLATE = "[table]\n[**]ID[||]Dorf[||]Besitzer[||]Punkte[/**]\n"
            + "%LIST_START%[*]%ELEMENT_ID%[|][coord]%X%|%Y%[/coord][|]%TRIBE%[|]%POINTS%[/*]%LIST_END%\n"
            + "[/table]";
    private final String TEMPLATE_PROPERTY = "village.list.bbexport.template";

    @Override
    public String getPropertyKey() {
        return TEMPLATE_PROPERTY;
    }

    @Override
    public String getStandardTemplate() {
        return STANDARD_TEMPLATE;
    }

    @Override
    public String formatElements(List<Village> pElements, boolean pExtended) {
        StringBuilder b = new StringBuilder();
        int cnt = 1;
        NumberFormat f = getNumberFormatter(pElements.size());
        String beforeList = getHeader();
        String listItemTemplate = getLineTemplate();
        String afterList = getFooter();
        String replacedStart = StringUtils.replaceEach(beforeList, new String[] { ELEMENT_COUNT },
                new String[] { f.format(pElements.size()) });
        b.append(replacedStart);
        for (Village v : pElements) {
            String[] replacements = v.getReplacements(pExtended);
            String itemLine = StringUtils.replaceEach(listItemTemplate, v.getBBVariables(), replacements);
            itemLine = StringUtils.replaceEach(itemLine, new String[] { ELEMENT_ID, ELEMENT_COUNT },
                    new String[] { f.format(cnt), f.format(pElements.size()) });
            b.append(itemLine).append("\n");
            cnt++;
        }
        String replacedEnd = StringUtils.replaceEach(afterList, new String[] { ELEMENT_COUNT },
                new String[] { f.format(pElements.size()) });
        b.append(replacedEnd);
        return b.toString();
    }

    @Override
    public String[] getTemplateVariables() {
        List<String> vars = new LinkedList<String>();
        for (String var : VARIABLES) {
            vars.add(var);
        }
        for (String var : new Village().getBBVariables()) {
            vars.add(var);
        }
        return vars.toArray(new String[vars.size()]);
    }
}