You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigTarget.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.config;
  23. import java.io.Serializable;
  24. /**
  25. * Represents the target of a particular config source.
  26. * <p>
  27. * Note: this class has a natural ordering that is inconsistent with equals.
  28. *
  29. * @author chris
  30. */
  31. public class ConfigTarget implements Comparable<ConfigTarget>, Serializable {
  32. /** The possible target types. */
  33. public static enum TYPE {
  34. /** Client-wide default settings. */
  35. GLOBALDEFAULT,
  36. /** Client-wide settings. */
  37. GLOBAL,
  38. /** Settings for a theme. */
  39. THEME,
  40. /** Settings for an IRCd. */
  41. IRCD,
  42. /** Settings for a network. */
  43. NETWORK,
  44. /** Settings for a server. */
  45. SERVER,
  46. /** Settings for a channel. */
  47. CHANNEL,
  48. /** Settings for a protocol (parser). */
  49. PROTOCOL,
  50. /** A custom identity, which doesn't contain settings to be loaded. */
  51. CUSTOM,
  52. }
  53. /**
  54. * A version number for this class. It should be changed whenever the class
  55. * structure is changed (or anything else that would prevent serialized
  56. * objects being unserialized with the new class).
  57. */
  58. private static final long serialVersionUID = 2;
  59. /** The type of this target. */
  60. protected TYPE type = ConfigTarget.TYPE.GLOBAL;
  61. /** The data of this target. */
  62. protected String data;
  63. /** The user-defined ordering for this target. */
  64. protected int order = 50000;
  65. /** Creates a new instance of ConfigTarget. */
  66. public ConfigTarget() {
  67. //Do nothing.
  68. }
  69. /**
  70. * Sets the ordering value for this target. Lower means higher preference.
  71. *
  72. * @param order The new order to use
  73. */
  74. public void setOrder(final int order) {
  75. this.order = order;
  76. }
  77. /**
  78. * Retrieves the ordering value for this target. Lower means higher preference.
  79. *
  80. * @return This target's order
  81. */
  82. public int getOrder() {
  83. return order;
  84. }
  85. /** Sets this target to be a global config source. */
  86. public void setGlobal() {
  87. type = TYPE.GLOBAL;
  88. data = "";
  89. }
  90. /** Sets this target to be a global default source. */
  91. public void setGlobalDefault() {
  92. type = TYPE.GLOBALDEFAULT;
  93. data = "";
  94. }
  95. /** Sets this target to be a theme source. */
  96. public void setTheme() {
  97. type = TYPE.THEME;
  98. data = "";
  99. }
  100. /**
  101. * Sets this target to be a custom identity.
  102. *
  103. * @param customType The type of custom identity
  104. * @since 0.6.4
  105. */
  106. public void setCustom(final String customType) {
  107. type = TYPE.CUSTOM;
  108. data = customType;
  109. }
  110. /**
  111. * Determines if this target is the specified custom type.
  112. *
  113. * @param customType The type of custom identity
  114. * @return True if this target is a CUSTOM type with the specified type.
  115. * @since 0.6.4
  116. */
  117. public boolean isCustom(final String customType) {
  118. return type == TYPE.CUSTOM && customType.equals(data);
  119. }
  120. /**
  121. * Sets this target to target an ircd.
  122. *
  123. * @param ircd The ircd to target
  124. */
  125. public void setIrcd(final String ircd) {
  126. type = TYPE.IRCD;
  127. data = ircd;
  128. }
  129. /**
  130. * Sets this target to target a network.
  131. *
  132. * @param network The network to target
  133. */
  134. public void setNetwork(final String network) {
  135. type = TYPE.NETWORK;
  136. data = network;
  137. }
  138. /**
  139. * Sets this target to target a server.
  140. *
  141. * @param server The server to target
  142. */
  143. public void setServer(final String server) {
  144. type = TYPE.SERVER;
  145. data = server;
  146. }
  147. /**
  148. * Sets this target to target a channel.
  149. *
  150. * @param channel The channel to target, in the form of channel@network
  151. */
  152. public void setChannel(final String channel) {
  153. type = TYPE.CHANNEL;
  154. data = channel;
  155. }
  156. /**
  157. * Sets this target to target a protocol.
  158. *
  159. * @param protocol The protocol to target
  160. * @since 0.6.3
  161. */
  162. public void setProtocol(final String protocol) {
  163. type = TYPE.PROTOCOL;
  164. data = protocol;
  165. }
  166. /**
  167. * Retrieves the type of this target.
  168. *
  169. * @return This target's type
  170. */
  171. public TYPE getType() {
  172. return type;
  173. }
  174. /**
  175. * Returns a string representation of the type of this target.
  176. *
  177. * @return A string describing this target's type
  178. */
  179. public String getTypeName() {
  180. return type.toString().toLowerCase();
  181. }
  182. /**
  183. * Retrieves the data associated with this target.
  184. *
  185. * @return This target's data
  186. */
  187. public String getData() {
  188. return data;
  189. }
  190. /** {@inheritDoc} */
  191. @Override
  192. public int hashCode() {
  193. return type.ordinal() + data.hashCode();
  194. }
  195. /** {@inheritDoc} */
  196. @Override
  197. public boolean equals(final Object obj) {
  198. return obj instanceof ConfigTarget
  199. && type == ((ConfigTarget) obj).getType()
  200. && data.equals(((ConfigTarget) obj).getData());
  201. }
  202. /**
  203. * Compares this target to another to determine which is more specific.
  204. *
  205. * @param target The target to compare to
  206. * @return a negative integer if this config is less specific, 0 if they're
  207. * equal, or a positive integer if this is more specific
  208. */
  209. @Override
  210. public int compareTo(final ConfigTarget target) {
  211. if (type.equals(target.getType())) {
  212. return target.getOrder() - order;
  213. } else {
  214. return type.compareTo(target.getType());
  215. }
  216. }
  217. /**
  218. * Returns a string representation of this object.
  219. *
  220. * @return A string representation of this object
  221. */
  222. @Override
  223. public String toString() {
  224. switch (type) {
  225. case GLOBALDEFAULT:
  226. return "Global defaults";
  227. case THEME:
  228. return "Theme";
  229. case CUSTOM:
  230. return "Custom: " + data;
  231. case IRCD:
  232. return "Ircd specific: " + data;
  233. case NETWORK:
  234. return "Network specific: " + data;
  235. case SERVER:
  236. return "Server specific: " + data;
  237. case CHANNEL:
  238. return "Channel specific: " + data;
  239. case PROTOCOL:
  240. return "Protocol specific: " + data;
  241. default:
  242. return "Global config";
  243. }
  244. }
  245. }