123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- /*
- * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * 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.dmdirc.config;
-
- import java.io.Serializable;
-
- /**
- * Represents the target of a particular config source.
- * <p>
- * Note: this class has a natural ordering that is inconsistent with equals.
- *
- * @author chris
- */
- public class ConfigTarget implements Comparable<ConfigTarget>, Serializable {
-
- /** The possible target types. */
- public static enum TYPE {
- /** Client-wide default settings. */
- GLOBALDEFAULT,
- /** Client-wide settings. */
- GLOBAL,
- /** Settings for a theme. */
- THEME,
- /** Settings for an IRCd. */
- IRCD,
- /** Settings for a network. */
- NETWORK,
- /** Settings for a server. */
- SERVER,
- /** Settings for a channel. */
- CHANNEL,
- /** Settings for a protocol (parser). */
- PROTOCOL,
- /** A custom identity, which doesn't contain settings to be loaded. */
- CUSTOM,
- }
-
- /**
- * A version number for this class. It should be changed whenever the class
- * structure is changed (or anything else that would prevent serialized
- * objects being unserialized with the new class).
- */
- private static final long serialVersionUID = 2;
-
- /** The type of this target. */
- protected TYPE type = ConfigTarget.TYPE.GLOBAL;
-
- /** The data of this target. */
- protected String data;
-
- /** The user-defined ordering for this target. */
- protected int order = 50000;
-
- /** Creates a new instance of ConfigTarget. */
- public ConfigTarget() {
- //Do nothing.
- }
-
- /**
- * Sets the ordering value for this target. Lower means higher preference.
- *
- * @param order The new order to use
- */
- public void setOrder(final int order) {
- this.order = order;
- }
-
- /**
- * Retrieves the ordering value for this target. Lower means higher preference.
- *
- * @return This target's order
- */
- public int getOrder() {
- return order;
- }
-
- /** Sets this target to be a global config source. */
- public void setGlobal() {
- type = TYPE.GLOBAL;
- data = "";
- }
-
- /** Sets this target to be a global default source. */
- public void setGlobalDefault() {
- type = TYPE.GLOBALDEFAULT;
- data = "";
- }
-
- /** Sets this target to be a theme source. */
- public void setTheme() {
- type = TYPE.THEME;
- data = "";
- }
-
- /**
- * Sets this target to be a custom identity.
- *
- * @param customType The type of custom identity
- * @since 0.6.4
- */
- public void setCustom(final String customType) {
- type = TYPE.CUSTOM;
- data = customType;
- }
-
- /**
- * Determines if this target is the specified custom type.
- *
- * @param customType The type of custom identity
- * @return True if this target is a CUSTOM type with the specified type.
- * @since 0.6.4
- */
- public boolean isCustom(final String customType) {
- return type == TYPE.CUSTOM && customType.equals(data);
- }
-
- /**
- * Sets this target to target an ircd.
- *
- * @param ircd The ircd to target
- */
- public void setIrcd(final String ircd) {
- type = TYPE.IRCD;
- data = ircd;
- }
-
- /**
- * Sets this target to target a network.
- *
- * @param network The network to target
- */
- public void setNetwork(final String network) {
- type = TYPE.NETWORK;
- data = network;
- }
-
- /**
- * Sets this target to target a server.
- *
- * @param server The server to target
- */
- public void setServer(final String server) {
- type = TYPE.SERVER;
- data = server;
- }
-
- /**
- * Sets this target to target a channel.
- *
- * @param channel The channel to target, in the form of channel@network
- */
- public void setChannel(final String channel) {
- type = TYPE.CHANNEL;
- data = channel;
- }
-
- /**
- * Sets this target to target a protocol.
- *
- * @param protocol The protocol to target
- * @since 0.6.3
- */
- public void setProtocol(final String protocol) {
- type = TYPE.PROTOCOL;
- data = protocol;
- }
-
- /**
- * Retrieves the type of this target.
- *
- * @return This target's type
- */
- public TYPE getType() {
- return type;
- }
-
- /**
- * Returns a string representation of the type of this target.
- *
- * @return A string describing this target's type
- */
- public String getTypeName() {
- return type.toString().toLowerCase();
- }
-
- /**
- * Retrieves the data associated with this target.
- *
- * @return This target's data
- */
- public String getData() {
- return data;
- }
-
- /** {@inheritDoc} */
- @Override
- public int hashCode() {
- return type.ordinal() + data.hashCode();
- }
-
- /** {@inheritDoc} */
- @Override
- public boolean equals(final Object obj) {
- return obj instanceof ConfigTarget
- && type == ((ConfigTarget) obj).getType()
- && data.equals(((ConfigTarget) obj).getData());
- }
-
- /**
- * Compares this target to another to determine which is more specific.
- *
- * @param target The target to compare to
- * @return a negative integer if this config is less specific, 0 if they're
- * equal, or a positive integer if this is more specific
- */
- @Override
- public int compareTo(final ConfigTarget target) {
- if (type.equals(target.getType())) {
- return target.getOrder() - order;
- } else {
- return type.compareTo(target.getType());
- }
- }
-
- /**
- * Returns a string representation of this object.
- *
- * @return A string representation of this object
- */
- @Override
- public String toString() {
- switch (type) {
- case GLOBALDEFAULT:
- return "Global defaults";
- case THEME:
- return "Theme";
- case CUSTOM:
- return "Custom: " + data;
- case IRCD:
- return "Ircd specific: " + data;
- case NETWORK:
- return "Network specific: " + data;
- case SERVER:
- return "Server specific: " + data;
- case CHANNEL:
- return "Channel specific: " + data;
- case PROTOCOL:
- return "Protocol specific: " + data;
- default:
- return "Global config";
- }
- }
-
- }
|