Java tutorial
/* Copyright (c) 2016 Kevin Wong * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.git.ifly6.nsapi.builders; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.tuple.MutablePair; import org.apache.commons.lang3.tuple.Pair; import com.git.ifly6.nsapi.NSConnection; /** @author Kevin */ public class NSNationQueryBuilder { private List<Pair<NSNationShard, Integer>> censusShards = new ArrayList<>(0); private List<NSNationShard> queryShards = new ArrayList<>(0); private StringBuilder builder = new StringBuilder(); public NSNationQueryBuilder(String nationName) { builder.append(NSConnection.API_PREFIX); builder.append("nation="); builder.append(nationName); builder.append("&q="); } public NSNationQueryBuilder addQuery(NSNationShard query) { addQuery(query, -1); return this; } public NSNationQueryBuilder addQuery(NSNationShard query, int censusId) { if (censusId != -1) { censusShards.add(new MutablePair<NSNationShard, Integer>(query, censusId)); } else { queryShards.add(query); } return this; } public NSNationQueryBuilder addQueries(NSNationShard[] queries) { for (NSNationShard query : queries) { addQuery(query); } return this; } /** Returns the relevant NS API <code>String</code> that can be used to create the relevant URL which can then call * the document desired. * @see java.lang.Object#toString() */ @Override public String toString() { for (NSNationShard shard : queryShards) { builder.append(shard.toString() + "+"); } if (censusShards.size() > 0) { List<Integer> cScores = new ArrayList<>(censusShards.size()); for (Pair<NSNationShard, Integer> pair : censusShards) { cScores.add(pair.getRight()); } builder.append("census;mode=score;scale="); for (Integer cScore : cScores) { builder.append(cScore.toString() + "+"); } } String output = builder.toString(); return output.endsWith("+") ? output.substring(0, output.length() - 1) : output; } }