Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Main {
    /**
     * Remove identical adjacent tags from {@code decorations}.
     * 
     * @param decorations see {@link prettify.parser.Job#decorations}
     * @param source the source code
     * 
     * @return the {@code decorations} after treatment
     * 
     * @throws IllegalArgumentException the size of {@code decoration} is not
     * a multiple of 2
     */
    public static List<Object> removeDuplicates(List<Object> decorations, String source) {
        if (decorations == null) {
            throw new NullPointerException("argument 'decorations' cannot be null");
        }
        if (source == null) {
            throw new NullPointerException("argument 'source' cannot be null");
        }
        if ((decorations.size() & 0x1) != 0) {
            throw new IllegalArgumentException("the size of argument 'decorations' should be a multiple of 2");
        }

        List<Object> returnList = new ArrayList<Object>();

        // use TreeMap to remove entrys with same pos
        Map<Integer, Object> orderedMap = new TreeMap<Integer, Object>();
        for (int i = 0, iEnd = decorations.size(); i < iEnd; i += 2) {
            orderedMap.put((Integer) decorations.get(i), decorations.get(i + 1));
        }

        // remove adjacent style
        String previousStyle = null;
        for (Integer pos : orderedMap.keySet()) {
            String style = (String) orderedMap.get(pos);
            if (previousStyle != null && previousStyle.equals(style)) {
                continue;
            }
            returnList.add(pos);
            returnList.add(style);
            previousStyle = style;
        }

        // remove last zero length tag
        int returnListSize = returnList.size();
        if (returnListSize >= 4 && returnList.get(returnListSize - 2).equals(source.length())) {
            returnList.remove(returnListSize - 2);
            returnList.remove(returnListSize - 2);
        }

        return returnList;
    }
}