Skip to content

Commit

Permalink
Merge pull request #31 from Agadar/dev-4.0.0
Browse files Browse the repository at this point in the history
Dev 4.0.0
  • Loading branch information
Agadar authored Mar 4, 2018
2 parents c35643a + d9bef3b commit 23e684b
Show file tree
Hide file tree
Showing 86 changed files with 4,129 additions and 522 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/target/
/nbproject/
/nbproject/
.classpath
.project
.settings/
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<!-- General properties -->
<groupId>com.github.agadar</groupId>
<artifactId>nationstates-api-java-wrapper</artifactId>
<version>3.0.0</version>
<version>4.0.0</version>
<packaging>jar</packaging>
<name>NationStates API Java Wrapper</name>
<description>A Java library for consuming the NationStates API.</description>
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/github/agadar/nationstates/INationStates.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.agadar.nationstates;

import com.github.agadar.nationstates.domain.nation.Nation;
import com.github.agadar.nationstates.domain.region.Region;
import com.github.agadar.nationstates.enumerator.Council;
import com.github.agadar.nationstates.enumerator.DailyDumpMode;
import com.github.agadar.nationstates.shard.WorldShard;
Expand All @@ -13,6 +15,8 @@
import com.github.agadar.nationstates.query.WorldAssemblyQuery;
import com.github.agadar.nationstates.query.WorldQuery;

import java.util.function.Predicate;

/**
* The starting point for consumers of this Java wrapper for the NationStates
* API.
Expand Down Expand Up @@ -112,9 +116,10 @@ public interface INationStates {
* Starts building a query that retrieves the daily region dump.
*
* @param mode the daily dump mode to use
* @param filter filter used for selecting regions
* @return a new daily region dump query
*/
public RegionDumpQuery getRegionDump(DailyDumpMode mode);
public RegionDumpQuery getRegionDump(DailyDumpMode mode, Predicate<Region> filter);

/**
* Starts building a query that retrieves the daily nation dump.
Expand All @@ -124,7 +129,8 @@ public interface INationStates {
* Nation objects being created from parsing the retrieved XML file.
*
* @param mode the daily dump mode to use
* @param filter filter used for selecting nations
* @return a new daily nation dump query
*/
public NationDumpQuery getNationDump(DailyDumpMode mode);
public NationDumpQuery getNationDump(DailyDumpMode mode, Predicate<Nation> filter);
}
21 changes: 13 additions & 8 deletions src/main/java/com/github/agadar/nationstates/NationStates.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.github.agadar.nationstates.xmlconverter.IXmlConverter;
import com.github.agadar.nationstates.enumerator.Council;
import com.github.agadar.nationstates.enumerator.DailyDumpMode;
import com.github.agadar.nationstates.domain.DailyDumpNations;
import com.github.agadar.nationstates.domain.DailyDumpRegions;
import com.github.agadar.nationstates.domain.nation.Nation;
import com.github.agadar.nationstates.domain.region.Region;
import com.github.agadar.nationstates.domain.worldassembly.WorldAssembly;
import com.github.agadar.nationstates.domain.world.World;
import com.github.agadar.nationstates.shard.WorldShard;
Expand All @@ -25,6 +25,8 @@
import java.io.File;
import java.net.URISyntaxException;
import java.security.CodeSource;

import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -101,8 +103,11 @@ public final class NationStates implements INationStates {
*/
public NationStates(String userAgent) {
this.setUserAgent(userAgent);
this.xmlConverter.registerTypes(DailyDumpNations.class, DailyDumpRegions.class,
World.class, WorldAssembly.class);
this.xmlConverter.registerTypes(
Nation.class,
Region.class,
World.class,
WorldAssembly.class);

try {
final CodeSource codeSource = this.getClass().getProtectionDomain().getCodeSource();
Expand Down Expand Up @@ -187,12 +192,12 @@ public TelegramQuery sendTelegrams(String clientKey, String telegramId, String s
}

@Override
public RegionDumpQuery getRegionDump(DailyDumpMode mode) {
return new RegionDumpQuery(xmlConverter, baseUrl, userAgent, defaultDumpDirectory, mode);
public RegionDumpQuery getRegionDump(DailyDumpMode mode, Predicate<Region> filter) {
return new RegionDumpQuery(baseUrl, userAgent, defaultDumpDirectory, mode, filter);
}

@Override
public NationDumpQuery getNationDump(DailyDumpMode mode) {
return new NationDumpQuery(xmlConverter, baseUrl, userAgent, defaultDumpDirectory, mode);
public NationDumpQuery getNationDump(DailyDumpMode mode, Predicate<Nation> filter) {
return new NationDumpQuery(baseUrl, userAgent, defaultDumpDirectory, mode, filter);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.agadar.nationstates.adapter;

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

import javax.xml.bind.annotation.adapters.XmlAdapter;

/**
* Converts a colon-separated String to a Set of Strings and vice versa.
*
* @author Agadar (https://github.com/Agadar/)
*/
public final class ColonSeparatedToSetAdapter extends XmlAdapter<String, Set<String>> {

@Override
public String marshal(Set<String> bt) {
return String.join(":", bt);
}

@Override
public Set<String> unmarshal(String vt) {
return new HashSet<>(Arrays.asList(vt.split(":")));
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.agadar.nationstates.adapter;

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

import javax.xml.bind.annotation.adapters.XmlAdapter;

/**
* Converts a comma-separated String to a Set of Strings and vice versa.
*
* @author Agadar (https://github.com/Agadar/)
*/
public final class CommaSeparatedToSetAdapter extends XmlAdapter<String, Set<String>> {

@Override
public String marshal(Set<String> bt) {
return String.join(",", bt);
}

@Override
public Set<String> unmarshal(String vt) {
return new HashSet<>(Arrays.asList(vt.split(",")));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.github.agadar.nationstates.adapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;

import com.github.agadar.nationstates.domain.common.happening.Happening;
import com.github.agadar.nationstates.happeningspecializer.ChangeHappeningSpecializer;
import com.github.agadar.nationstates.happeningspecializer.DispatchHappeningSpecializer;
import com.github.agadar.nationstates.happeningspecializer.EjectedHappeningSpecializer;
import com.github.agadar.nationstates.happeningspecializer.EmbassyHappeningSpecializer;
import com.github.agadar.nationstates.happeningspecializer.IHappeningSpecializer;
import com.github.agadar.nationstates.happeningspecializer.LawHappeningSpecializer;

/**
* Takes a collection of happenings and returns a sorted set containing those
* same happenings, specialized to corresponding subclasses where applicable.
*
* @author Agadar (https://github.com/Agadar/)
*
*/
public class HappeningSpecializationHelper implements IHappeningSpecializer<Happening> {

private static List<IHappeningSpecializer> happeningSpecializers;
private static HappeningSpecializationHelper instance;

static {
happeningSpecializers = new ArrayList<>();
happeningSpecializers.add(new ChangeHappeningSpecializer());
happeningSpecializers.add(new DispatchHappeningSpecializer());
happeningSpecializers.add(new EjectedHappeningSpecializer());
happeningSpecializers.add(new EmbassyHappeningSpecializer());
happeningSpecializers.add(new LawHappeningSpecializer());
instance = new HappeningSpecializationHelper();
}

/**
* Takes a collection of happenings and returns a sorted set containing those
* same happenings, specialized to corresponding subclasses where applicable.
*
* @param happenings
* @return
*/
public static SortedSet<Happening> specializeHappenings(Collection<Happening> happenings) {
if (happenings == null) {
return null;
}
return new TreeSet(happenings.stream()
.map(happening -> happeningSpecializers.stream()
.filter(specializer -> specializer.isOfSpecializedType(happening)).findAny().orElse(instance)
.toSpecializedType(happening))
.collect(Collectors.toSet()));
}

@Override
public boolean isOfSpecializedType(Happening happening) {
throw new UnsupportedOperationException("This method should not have been called");
}

@Override
public Happening toSpecializedType(Happening happening) {
return happening;
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.agadar.nationstates.domain.common;

import java.util.List;
import java.util.SortedSet;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
Expand Down Expand Up @@ -64,5 +64,28 @@ public class CensusScore {
* save for Id will not be retrieved.
*/
@XmlElement(name = "POINT")
public List<CensusScoreHistory> history;
public SortedSet<CensusScoreHistory> history;

@Override
public int hashCode() {
int hash = 7;
hash = 37 * hash + this.id;
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final CensusScore other = (CensusScore) obj;
return this.id == other.id;
}

}
Loading

0 comments on commit 23e684b

Please sign in to comment.