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

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