mirror of
https://github.com/BGHDDevelopment/PlayerBalancer.git
synced 2024-11-09 04:20:32 +01:00
Some little changes
This commit is contained in:
parent
0ee5d5538b
commit
1538f48159
@ -11,9 +11,8 @@
|
|||||||
### Things to do:
|
### Things to do:
|
||||||
- [ ] Create a spigot addon that adds connector signs and placeholders
|
- [ ] Create a spigot addon that adds connector signs and placeholders
|
||||||
- [ ] Separate the types of connections in classes instead of being in ConnectionIntent
|
- [ ] Separate the types of connections in classes instead of being in ConnectionIntent
|
||||||
- [ ] Make the plugin API be not so dependent on a instance of PlayerBalancer
|
- [x] Make the plugin API be not so dependent on a instance of PlayerBalancer
|
||||||
- [ ] Separate connection providers in classes instead of being hardcoded in an enum
|
- [ ] Separate connection providers in classes instead of being hardcoded in an enum
|
||||||
- [ ] Make the feature `marker-descs` work per section
|
|
||||||
- [ ] Implement fast connect (dimension change)
|
- [ ] Implement fast connect (dimension change)
|
||||||
- [ ] Implement a way to redirect premium players to a section and cracked ones to other section (not sure how this works)
|
- [ ] Implement a way to redirect premium players to a section and cracked ones to other section (not sure how this works)
|
||||||
- [x] Unify the code that loads serverName into a section (duplicated at SectionManager and ServerSection)
|
- [x] Unify the code that loads serverName into a section (duplicated at SectionManager and ServerSection)
|
||||||
|
@ -79,6 +79,20 @@ public class ManageCommand extends Command {
|
|||||||
.color(manager.isPrincipal(section) ? ChatColor.GREEN : ChatColor.RED)
|
.color(manager.isPrincipal(section) ? ChatColor.GREEN : ChatColor.RED)
|
||||||
.create());
|
.create());
|
||||||
|
|
||||||
|
sender.sendMessage(new ComponentBuilder("Dummy: ")
|
||||||
|
.color(ChatColor.GRAY)
|
||||||
|
.append(manager.isDummy(section) ? "yes" : "no")
|
||||||
|
.color(manager.isDummy(section) ? ChatColor.GREEN : ChatColor.RED)
|
||||||
|
.create()
|
||||||
|
);
|
||||||
|
|
||||||
|
sender.sendMessage(new ComponentBuilder("Reiterative: ")
|
||||||
|
.color(ChatColor.GRAY)
|
||||||
|
.append(manager.isReiterative(section) ? "yes" : "no")
|
||||||
|
.color(manager.isReiterative(section) ? ChatColor.GREEN : ChatColor.RED)
|
||||||
|
.create()
|
||||||
|
);
|
||||||
|
|
||||||
if (section.getParent() != null) {
|
if (section.getParent() != null) {
|
||||||
sender.sendMessage(new ComponentBuilder("Parent: ")
|
sender.sendMessage(new ComponentBuilder("Parent: ")
|
||||||
.color(ChatColor.GRAY)
|
.color(ChatColor.GRAY)
|
||||||
@ -111,20 +125,6 @@ public class ManageCommand extends Command {
|
|||||||
.create()
|
.create()
|
||||||
);
|
);
|
||||||
|
|
||||||
sender.sendMessage(new ComponentBuilder("Dummy: ")
|
|
||||||
.color(ChatColor.GRAY)
|
|
||||||
.append(manager.isDummy(section) ? "yes" : "no")
|
|
||||||
.color(manager.isDummy(section) ? ChatColor.GREEN : ChatColor.RED)
|
|
||||||
.create()
|
|
||||||
);
|
|
||||||
|
|
||||||
sender.sendMessage(new ComponentBuilder("Reiterative: ")
|
|
||||||
.color(ChatColor.GRAY)
|
|
||||||
.append(manager.isReiterative(section) ? "yes" : "no")
|
|
||||||
.color(manager.isReiterative(section) ? ChatColor.GREEN : ChatColor.RED)
|
|
||||||
.create()
|
|
||||||
);
|
|
||||||
|
|
||||||
if (section.getServer() != null) {
|
if (section.getServer() != null) {
|
||||||
sender.sendMessage(new ComponentBuilder("Section Server: ")
|
sender.sendMessage(new ComponentBuilder("Section Server: ")
|
||||||
.color(ChatColor.GRAY)
|
.color(ChatColor.GRAY)
|
||||||
|
@ -14,6 +14,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
//TODO I don't like this, improve it
|
||||||
public abstract class ConnectionIntent {
|
public abstract class ConnectionIntent {
|
||||||
protected final PlayerBalancer plugin;
|
protected final PlayerBalancer plugin;
|
||||||
protected final ProxiedPlayer player;
|
protected final ProxiedPlayer player;
|
||||||
|
@ -13,18 +13,18 @@ import java.util.concurrent.ThreadLocalRandom;
|
|||||||
public enum ProviderType {
|
public enum ProviderType {
|
||||||
NONE {
|
NONE {
|
||||||
@Override
|
@Override
|
||||||
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> list, ProxiedPlayer player) {
|
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
LOWEST {
|
LOWEST {
|
||||||
@Override
|
@Override
|
||||||
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> list, ProxiedPlayer player) {
|
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
|
||||||
int min = Integer.MAX_VALUE;
|
int min = Integer.MAX_VALUE;
|
||||||
ServerInfo target = null;
|
ServerInfo target = null;
|
||||||
|
|
||||||
for (ServerInfo server : list) {
|
for (ServerInfo server : servers) {
|
||||||
int count = plugin.getNetworkManager().getPlayers(server);
|
int count = plugin.getNetworkManager().getPlayers(server);
|
||||||
|
|
||||||
if (count < min) {
|
if (count < min) {
|
||||||
@ -39,11 +39,11 @@ public enum ProviderType {
|
|||||||
|
|
||||||
BALANCED {
|
BALANCED {
|
||||||
@Override
|
@Override
|
||||||
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> list, ProxiedPlayer player) {
|
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
|
||||||
List<ServerInfo> results = new ArrayList<>();
|
List<ServerInfo> results = new ArrayList<>();
|
||||||
int min = Integer.MAX_VALUE;
|
int min = Integer.MAX_VALUE;
|
||||||
|
|
||||||
for (ServerInfo server : list) {
|
for (ServerInfo server : servers) {
|
||||||
int count = plugin.getNetworkManager().getPlayers(server);
|
int count = plugin.getNetworkManager().getPlayers(server);
|
||||||
|
|
||||||
if (count <= min) {
|
if (count <= min) {
|
||||||
@ -55,38 +55,38 @@ public enum ProviderType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return results.get(ThreadLocalRandom.current().nextInt(list.size()));
|
return results.get(ThreadLocalRandom.current().nextInt(servers.size()));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
RANDOM {
|
RANDOM {
|
||||||
@Override
|
@Override
|
||||||
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> list, ProxiedPlayer player) {
|
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
|
||||||
return list.get(ThreadLocalRandom.current().nextInt(list.size()));
|
return servers.get(ThreadLocalRandom.current().nextInt(servers.size()));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
PROGRESSIVE {
|
PROGRESSIVE {
|
||||||
@Override
|
@Override
|
||||||
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> list, ProxiedPlayer player) {
|
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
|
||||||
for (ServerInfo server : list) {
|
for (ServerInfo server : servers) {
|
||||||
ServerStatus status = plugin.getStatusManager().getStatus(server);
|
ServerStatus status = plugin.getStatusManager().getStatus(server);
|
||||||
if (plugin.getNetworkManager().getPlayers(server) < status.getMaximum()) {
|
if (plugin.getNetworkManager().getPlayers(server) < status.getMaximum()) {
|
||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return list.get(ThreadLocalRandom.current().nextInt(list.size()));
|
return servers.get(ThreadLocalRandom.current().nextInt(servers.size()));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
FILLER {
|
FILLER {
|
||||||
@Override
|
@Override
|
||||||
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> list, ProxiedPlayer player) {
|
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
|
||||||
int max = Integer.MIN_VALUE;
|
int max = Integer.MIN_VALUE;
|
||||||
ServerInfo target = null;
|
ServerInfo target = null;
|
||||||
|
|
||||||
for (ServerInfo server : list) {
|
for (ServerInfo server : servers) {
|
||||||
ServerStatus status = plugin.getStatusManager().getStatus(server);
|
ServerStatus status = plugin.getStatusManager().getStatus(server);
|
||||||
int count = plugin.getNetworkManager().getPlayers(server);
|
int count = plugin.getNetworkManager().getPlayers(server);
|
||||||
|
|
||||||
@ -100,5 +100,5 @@ public enum ProviderType {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
public abstract ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> list, ProxiedPlayer player);
|
public abstract ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player);
|
||||||
}
|
}
|
@ -11,21 +11,21 @@ import java.util.Map;
|
|||||||
public class ServerAssignRegistry {
|
public class ServerAssignRegistry {
|
||||||
private static final Table<ProxiedPlayer, ServerSection, ServerInfo> table = HashBasedTable.create();
|
private static final Table<ProxiedPlayer, ServerSection, ServerInfo> table = HashBasedTable.create();
|
||||||
|
|
||||||
public static void assignTarget(ProxiedPlayer player, ServerSection group, ServerInfo server) {
|
public static void assignTarget(ProxiedPlayer player, ServerSection section, ServerInfo server) {
|
||||||
synchronized (table) {
|
synchronized (table) {
|
||||||
table.put(player, group, server);
|
table.put(player, section, server);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void revokeTarget(ProxiedPlayer player, ServerSection group) {
|
public static void revokeTarget(ProxiedPlayer player, ServerSection section) {
|
||||||
synchronized (table) {
|
synchronized (table) {
|
||||||
table.remove(player, group);
|
table.remove(player, section);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ServerInfo getAssignedServer(ProxiedPlayer player, ServerSection group) {
|
public static ServerInfo getAssignedServer(ProxiedPlayer player, ServerSection section) {
|
||||||
synchronized (table) {
|
synchronized (table) {
|
||||||
return table.get(player, group);
|
return table.get(player, section);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,9 +41,9 @@ public class ServerAssignRegistry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean hasAssignedServer(ProxiedPlayer player, ServerSection group) {
|
public static boolean hasAssignedServer(ProxiedPlayer player, ServerSection section) {
|
||||||
synchronized (table) {
|
synchronized (table) {
|
||||||
return table.contains(player, group);
|
return table.contains(player, section);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,23 +2,16 @@ package com.jaimemartz.playerbalancer.section;
|
|||||||
|
|
||||||
import com.jaimemartz.playerbalancer.PlayerBalancer;
|
import com.jaimemartz.playerbalancer.PlayerBalancer;
|
||||||
import com.jaimemartz.playerbalancer.commands.FallbackCommand;
|
import com.jaimemartz.playerbalancer.commands.FallbackCommand;
|
||||||
import com.jaimemartz.playerbalancer.settings.props.shared.CommandProps;
|
|
||||||
import net.md_5.bungee.api.CommandSender;
|
|
||||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||||
|
|
||||||
public class SectionCommand extends FallbackCommand {
|
public class SectionCommand extends FallbackCommand {
|
||||||
private final ServerSection section;
|
private final ServerSection section;
|
||||||
|
|
||||||
public SectionCommand(PlayerBalancer plugin, CommandProps props, ServerSection section) {
|
public SectionCommand(PlayerBalancer plugin, ServerSection section) {
|
||||||
super(plugin, props);
|
super(plugin, section.getProps().getCommandProps());
|
||||||
this.section = section;
|
this.section = section;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void execute(CommandSender sender, String[] args) {
|
|
||||||
super.execute(sender, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ServerSection getSection(ProxiedPlayer player) {
|
public ServerSection getSection(ProxiedPlayer player) {
|
||||||
return section;
|
return section;
|
||||||
|
@ -3,7 +3,6 @@ package com.jaimemartz.playerbalancer.section;
|
|||||||
import com.jaimemartz.playerbalancer.PlayerBalancer;
|
import com.jaimemartz.playerbalancer.PlayerBalancer;
|
||||||
import com.jaimemartz.playerbalancer.settings.props.features.BalancerProps;
|
import com.jaimemartz.playerbalancer.settings.props.features.BalancerProps;
|
||||||
import com.jaimemartz.playerbalancer.settings.props.shared.SectionProps;
|
import com.jaimemartz.playerbalancer.settings.props.shared.SectionProps;
|
||||||
import com.jaimemartz.playerbalancer.utils.FakeServer;
|
|
||||||
import com.jaimemartz.playerbalancer.utils.FixedAdapter;
|
import com.jaimemartz.playerbalancer.utils.FixedAdapter;
|
||||||
import net.md_5.bungee.api.config.ServerInfo;
|
import net.md_5.bungee.api.config.ServerInfo;
|
||||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||||
@ -208,7 +207,7 @@ public class SectionManager {
|
|||||||
@Override
|
@Override
|
||||||
public void execute(String sectionName, SectionProps sectionProps, ServerSection section) throws RuntimeException {
|
public void execute(String sectionName, SectionProps sectionProps, ServerSection section) throws RuntimeException {
|
||||||
if (sectionProps.getServerName() != null) {
|
if (sectionProps.getServerName() != null) {
|
||||||
FakeServer server = new FakeServer(section);
|
SectionServer server = new SectionServer(section);
|
||||||
section.setServer(server);
|
section.setServer(server);
|
||||||
plugin.getSectionManager().register(server, section);
|
plugin.getSectionManager().register(server, section);
|
||||||
FixedAdapter.getFakeServers().put(server.getName(), server);
|
FixedAdapter.getFakeServers().put(server.getName(), server);
|
||||||
@ -219,8 +218,8 @@ public class SectionManager {
|
|||||||
new SectionStage("Section command processing") {
|
new SectionStage("Section command processing") {
|
||||||
@Override
|
@Override
|
||||||
public void execute(String sectionName, SectionProps sectionProps, ServerSection section) throws RuntimeException {
|
public void execute(String sectionName, SectionProps sectionProps, ServerSection section) throws RuntimeException {
|
||||||
if (sectionProps.getCommand() != null) {
|
if (sectionProps.getCommandProps() != null) {
|
||||||
SectionCommand command = new SectionCommand(plugin, sectionProps.getCommand(), section);
|
SectionCommand command = new SectionCommand(plugin, section);
|
||||||
section.setCommand(command);
|
section.setCommand(command);
|
||||||
plugin.getProxy().getPluginManager().registerCommand(plugin, command);
|
plugin.getProxy().getPluginManager().registerCommand(plugin, command);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.jaimemartz.playerbalancer.utils;
|
package com.jaimemartz.playerbalancer.section;
|
||||||
|
|
||||||
import com.jaimemartz.playerbalancer.section.ServerSection;
|
|
||||||
import net.md_5.bungee.BungeeServerInfo;
|
import net.md_5.bungee.BungeeServerInfo;
|
||||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||||
|
|
||||||
@ -9,10 +8,10 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class FakeServer extends BungeeServerInfo {
|
public class SectionServer extends BungeeServerInfo {
|
||||||
private final ServerSection section;
|
private final ServerSection section;
|
||||||
|
|
||||||
public FakeServer(ServerSection section) {
|
public SectionServer(ServerSection section) {
|
||||||
|
|
||||||
super(
|
super(
|
||||||
"@" + section.getProps().getServerName(),
|
"@" + section.getProps().getServerName(),
|
@ -27,8 +27,9 @@ public class ServerSection {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
this.props = props;
|
this.props = props;
|
||||||
|
|
||||||
AlphanumComparator<ServerInfo> comparator = new AlphanumComparator<>();
|
this.servers = Collections.synchronizedSortedSet(new TreeSet<>((lhs, rhs) ->
|
||||||
this.servers = Collections.synchronizedSortedSet(new TreeSet<>(comparator));
|
AlphanumComparator.getInstance().compare(lhs.getName(), rhs.getName())
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -17,8 +17,8 @@ public class SectionProps {
|
|||||||
@Setting(value = "servers")
|
@Setting(value = "servers")
|
||||||
private List<String> serverEntries;
|
private List<String> serverEntries;
|
||||||
|
|
||||||
@Setting(value = "section-command")
|
@Setting(value = "section-commandProps")
|
||||||
private CommandProps command;
|
private CommandProps commandProps;
|
||||||
|
|
||||||
@Setting(value = "section-server")
|
@Setting(value = "section-server")
|
||||||
private String serverName;
|
private String serverName;
|
||||||
@ -47,12 +47,12 @@ public class SectionProps {
|
|||||||
this.serverEntries = serverEntries;
|
this.serverEntries = serverEntries;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandProps getCommand() {
|
public CommandProps getCommandProps() {
|
||||||
return command;
|
return commandProps;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCommand(CommandProps command) {
|
public void setCommandProps(CommandProps commandProps) {
|
||||||
this.command = command;
|
this.commandProps = commandProps;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getServerName() {
|
public String getServerName() {
|
||||||
@ -69,7 +69,7 @@ public class SectionProps {
|
|||||||
"provider=" + provider +
|
"provider=" + provider +
|
||||||
", parentName='" + parentName + '\'' +
|
", parentName='" + parentName + '\'' +
|
||||||
", serverEntries=" + serverEntries +
|
", serverEntries=" + serverEntries +
|
||||||
", command=" + command +
|
", commandProps=" + commandProps +
|
||||||
", serverName='" + serverName + '\'' +
|
", serverName='" + serverName + '\'' +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
@ -7,43 +7,44 @@ package com.jaimemartz.playerbalancer.utils;
|
|||||||
*
|
*
|
||||||
* The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
|
* The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
|
||||||
*
|
*
|
||||||
|
* Released under the MIT License - https://opensource.org/licenses/MIT
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* Copyright 2007-2017 David Koelle
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
|
||||||
* License as published by the Free Software Foundation; either
|
|
||||||
* version 2.1 of the License, or any later version.
|
|
||||||
*
|
*
|
||||||
* This library is distributed in the hope that it will be useful,
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* a copy of this software and associated documentation files (the "Software"),
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
* to deal in the Software without restriction, including without limitation
|
||||||
* Lesser General Public License for more details.
|
* 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:
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU Lesser General Public
|
* The above copyright notice and this permission notice shall be included
|
||||||
* License along with this library; if not, write to the Free Software
|
* in all copies or substantial portions of the Software.
|
||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*
|
*
|
||||||
|
* 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.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is an updated version with enhancements made by Daniel Migowski,
|
* This is an updated version with enhancements made by Daniel Migowski,
|
||||||
* Andre Bogus, and David Koelle
|
* Andre Bogus, and David Koelle. Updated by David Koelle in 2017.
|
||||||
*
|
|
||||||
* To convert to use Templates (Java 1.5+):
|
|
||||||
* - Change "implements Comparator" to "implements Comparator<String>"
|
|
||||||
* - Change "compare(Object o1, Object o2)" to "compare(String s1, String s2)"
|
|
||||||
* - Remove the type checking and casting in compare().
|
|
||||||
*
|
*
|
||||||
* To use this class:
|
* To use this class:
|
||||||
* Use the static "sort" method from the java.util.Collections class:
|
* Use the static "sort" method from the java.util.Collections class:
|
||||||
* Collections.sort(your list, new AlphanumComparator());
|
* Collections.sort(your list, new AlphanumComparator());
|
||||||
*/
|
*/
|
||||||
public final class AlphanumComparator<T> implements Comparator<T>
|
public class AlphanumComparator implements Comparator<String>
|
||||||
{
|
{
|
||||||
private final boolean isDigit(char ch)
|
private final boolean isDigit(char ch)
|
||||||
{
|
{
|
||||||
return ch >= 48 && ch <= 57;
|
return ((ch >= 48) && (ch <= 57));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Length of string is passed in for improved efficiency (only need to calculate it once) **/
|
/** Length of string is passed in for improved efficiency (only need to calculate it once) **/
|
||||||
@ -77,14 +78,12 @@ public final class AlphanumComparator<T> implements Comparator<T>
|
|||||||
return chunk.toString();
|
return chunk.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int compare(Object o1, Object o2)
|
public int compare(String s1, String s2)
|
||||||
{
|
{
|
||||||
if (!(o1 instanceof String) || !(o2 instanceof String))
|
if ((s1 == null) || (s2 == null))
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
String s1 = (String)o1;
|
|
||||||
String s2 = (String)o2;
|
|
||||||
|
|
||||||
int thisMarker = 0;
|
int thisMarker = 0;
|
||||||
int thatMarker = 0;
|
int thatMarker = 0;
|
||||||
@ -118,7 +117,8 @@ public final class AlphanumComparator<T> implements Comparator<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
result = thisChunk.compareTo(thatChunk);
|
result = thisChunk.compareTo(thatChunk);
|
||||||
}
|
}
|
||||||
@ -129,4 +129,9 @@ public final class AlphanumComparator<T> implements Comparator<T>
|
|||||||
|
|
||||||
return s1Length - s2Length;
|
return s1Length - s2Length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final AlphanumComparator instance = new AlphanumComparator();
|
||||||
|
public static AlphanumComparator getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user