ru.jcorp.smartstreets.helpers.Bundle.java Source code

Java tutorial

Introduction

Here is the source code for ru.jcorp.smartstreets.helpers.Bundle.java

Source

/*
 * This file is part of SmartStreets.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package ru.jcorp.smartstreets.helpers;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.lang.StringUtils;
import ru.jcorp.smartstreets.map.SmartMap;
import ru.jcorp.smartstreets.map.SmartMapLine;
import ru.jcorp.smartstreets.map.SmartMapNode;

import java.util.HashSet;
import java.util.Set;

/**
 * <p>$Id$</p>
 *
 * @author Artamonov Yuriy
 */
public class Bundle {

    public static class MapBuilder {

        private SmartMap map = new SmartMap();

        public MapBuilder setPointCount(int count) {
            Set<SmartMapNode> nodeSet = new HashSet<SmartMapNode>();
            for (int i = 1; i <= count; i++) {
                nodeSet.add(new SmartMapNode(map, Integer.toString(i)));
            }
            map.setNodes(nodeSet);
            return this;
        }

        public MapBuilder setRelations(LineDefinition... relations) {
            if (map.getNodes() != null) {
                for (LineDefinition lineDef : relations) {
                    SmartMapNode startNode = getPointByName(map, lineDef.getFrom());
                    SmartMapNode endNode = getPointByName(map, lineDef.getTo());

                    if ((startNode != null) && (endNode != null)) {
                        SmartMapLine line = new SmartMapLine(startNode, endNode, lineDef.getWeight());

                        if (startNode.getStartings() == null)
                            startNode.setStartings(new HashSet<SmartMapLine>());
                        startNode.getStartings().add(line);

                        if (endNode.getEndings() == null)
                            endNode.setEndings(new HashSet<SmartMapLine>());
                        endNode.getEndings().add(line);
                    }
                }
            }
            return this;
        }

        public SmartMap draw() {
            SmartMap resultMap = map;
            map = new SmartMap();
            return resultMap;
        }
    }

    public static class LineDefinition {

        private String from;

        private String to;

        private double weight;

        public LineDefinition(String from, String to, double weight) {
            this.from = from;
            this.to = to;
            this.weight = weight;
        }

        public String getFrom() {
            return from;
        }

        public String getTo() {
            return to;
        }

        public double getWeight() {
            return weight;
        }
    }

    public static class PointDefinition {
        private String name;

        public PointDefinition(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

    private static SmartMapNode getPointByName(SmartMap map, final String pointName) {
        return (SmartMapNode) CollectionUtils.find(map.getNodes(), new Predicate() {
            @Override
            public boolean evaluate(Object object) {
                SmartMapNode node = (SmartMapNode) object;
                return StringUtils.equals(node.getTitle(), pointName);
            }
        });
    }

    public static PointDefinition node(String pointName) {
        return new PointDefinition(pointName);
    }

    public static LineDefinition line(String from, String to, double weight) {
        return new LineDefinition(from, to, weight);
    }

    public static SmartMapNode point(SmartMap map, String pointName) {
        return getPointByName(map, pointName);
    }

    public static MapBuilder routingMap() {
        return new MapBuilder();
    }
}