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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2006-2007 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 final class ConfigTarget implements Comparable, 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 a profile. */
  41. PROFILE,
  42. /** Settings for an IRCd. */
  43. IRCD,
  44. /** Settings for a network. */
  45. NETWORK,
  46. /** Settings for a server. */
  47. SERVER,
  48. /** Settings for a channel. */
  49. CHANNEL,
  50. }
  51. /**
  52. * A version number for this class. It should be changed whenever the class
  53. * structure is changed (or anything else that would prevent serialized
  54. * objects being unserialized with the new class).
  55. */
  56. private static final long serialVersionUID = 2;
  57. /** The type of this target. */
  58. private TYPE type = ConfigTarget.TYPE.GLOBAL;
  59. /** The data of this target. */
  60. private String data;
  61. /** The user-defined ordering for this target. */
  62. private int order = 50000;
  63. /** Creates a new instance of ConfigTarget. */
  64. public ConfigTarget() {
  65. //Do nothing.
  66. }
  67. /**
  68. * Sets the ordering value for this target. Lower means higher preference.
  69. *
  70. * @param order The new order to use
  71. */
  72. public void setOrder(final int order) {
  73. this.order = order;
  74. }
  75. /**
  76. * Retrieves the ordering value for this target. Lower means higher preference.
  77. *
  78. * @return This target's order
  79. */
  80. public int getOrder() {
  81. return order;
  82. }
  83. /** Sets this target to be a global config source. */
  84. public void setGlobal() {
  85. type = TYPE.GLOBAL;
  86. data = "";
  87. }
  88. /** Sets this target to be a global default source. */
  89. public void setGlobalDefault() {
  90. type = TYPE.GLOBALDEFAULT;
  91. data = "";
  92. }
  93. /** Sets this target to be a theme source. */
  94. public void setTheme() {
  95. type = TYPE.THEME;
  96. data = "";
  97. }
  98. /** Sets this target to be a profile source. */
  99. public void setProfile() {
  100. type = TYPE.PROFILE;
  101. data = "";
  102. }
  103. /**
  104. * Sets this target to target an ircd.
  105. *
  106. * @param ircd The ircd to target
  107. */
  108. public void setIrcd(final String ircd) {
  109. type = TYPE.IRCD;
  110. data = ircd;
  111. }
  112. /**
  113. * Sets this target to target a network.
  114. *
  115. * @param network The network to target
  116. */
  117. public void setNetwork(final String network) {
  118. type = TYPE.NETWORK;
  119. data = network;
  120. }
  121. /**
  122. * Sets this target to target a server.
  123. *
  124. * @param server The server to target
  125. */
  126. public void setServer(final String server) {
  127. type = TYPE.SERVER;
  128. data = server;
  129. }
  130. /**
  131. * Sets this target to target a channel.
  132. *
  133. * @param channel The channel to target, in the form of channel@network
  134. */
  135. public void setChannel(final String channel) {
  136. type = TYPE.CHANNEL;
  137. data = channel;
  138. }
  139. /**
  140. * Retrieves the type of this target.
  141. *
  142. * @return This target's type
  143. */
  144. public TYPE getType() {
  145. return type;
  146. }
  147. /**
  148. * Returns a string representation of the type of this target.
  149. *
  150. * @return A string describing this target's type
  151. */
  152. public String getTypeName() {
  153. switch(type) {
  154. case GLOBALDEFAULT:
  155. return "globaldefault";
  156. case GLOBAL:
  157. return "global";
  158. case THEME:
  159. return "theme";
  160. case PROFILE:
  161. return "profile";
  162. case IRCD:
  163. return "ircd";
  164. case NETWORK:
  165. return "network";
  166. case SERVER:
  167. return "server";
  168. case CHANNEL:
  169. return "channel";
  170. default:
  171. return "Unknown";
  172. }
  173. }
  174. /**
  175. * Retrieves the data associated with this target.
  176. *
  177. * @return This target's data
  178. */
  179. public String getData() {
  180. return data;
  181. }
  182. /** {@inheritDoc} */
  183. @Override
  184. public int hashCode() {
  185. return type.ordinal() + data.hashCode();
  186. }
  187. /** {@inheritDoc} */
  188. @Override
  189. public boolean equals(final Object obj) {
  190. if (obj instanceof ConfigTarget
  191. && type == ((ConfigTarget) obj).getType()
  192. && data.equals(((ConfigTarget) obj).getData())) {
  193. return true;
  194. }
  195. return false;
  196. }
  197. /**
  198. * Compares this target to another to determine which is more specific.
  199. *
  200. * @param target The target to compare to
  201. * @return a negative integer if this config is less specific, 0 if they're
  202. * equal, or a positive integer if this is more specific
  203. */
  204. public int compareTo(final Object target) {
  205. if (type.equals(((ConfigTarget) target).getType())) {
  206. return ((ConfigTarget) target).getOrder() - order;
  207. } else {
  208. return type.compareTo(((ConfigTarget) target).getType());
  209. }
  210. }
  211. /**
  212. * Returns a string representation of this object.
  213. *
  214. * @return A string representation of this object
  215. */
  216. @Override
  217. public String toString() {
  218. switch (type) {
  219. case GLOBALDEFAULT:
  220. return "Global defaults";
  221. case GLOBAL:
  222. return "Global config";
  223. case THEME:
  224. return "Theme";
  225. case PROFILE:
  226. return "Profile";
  227. case IRCD:
  228. return "Ircd specific: " + data;
  229. case NETWORK:
  230. return "Network specific: " + data;
  231. case SERVER:
  232. return "Server specific: " + data;
  233. case CHANNEL:
  234. return "Channel specific: " + data;
  235. default:
  236. return "Unknown";
  237. }
  238. }
  239. }