1 /** 2 * Copyright 2005 Steve Molloy 3 * 4 * This file is part of OV4J. 5 * 6 * OV4J is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as 7 * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 8 * 9 * OV4J is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License along with OV4J; if not, write to the Free Software 13 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 14 * 15 */ 16 package org.ov4j.cache; 17 18 import java.io.Serializable; 19 import java.lang.reflect.Array; 20 import java.util.logging.Level; 21 import java.util.logging.Logger; 22 23 /** 24 * ID used for storing and retrieving results in the cache. 25 * 26 * @author smolloy 27 * 28 */ 29 public class CachedResultId implements Comparable<CachedResultId>, Serializable { 30 /** 31 * Logger for this class 32 */ 33 private static final Logger logger = Logger.getLogger(CachedResultId.class.getName()); 34 35 /** Serial UID */ 36 private static final long serialVersionUID = -6165070370231138090L; 37 38 /** Method for which result is stored. */ 39 private String method; 40 41 /** Arguments used for the method call. */ 42 private Object args; 43 44 /** 45 * Constructor. 46 */ 47 public CachedResultId() { 48 } 49 50 /** 51 * @see java.lang.Comparable#compareTo(T) 52 */ 53 public int compareTo(final CachedResultId o) { 54 if (CachedResultId.logger.isLoggable(Level.FINEST)) { 55 CachedResultId.logger.entering("CachedResultId", "compareTo(CachedResultId=" + o + ")", "start"); 56 } 57 58 if (o == null) { 59 if (CachedResultId.logger.isLoggable(Level.FINEST)) { 60 CachedResultId.logger.exiting("CachedResultId", "compareTo(CachedResultId=null)", 61 "end - return value=" + 1); 62 } 63 return 1; 64 } 65 66 int res = ((method == null) ? "" : method).compareTo((o.getMethod() == null) ? "" : o.getMethod()); 67 if (res == 0) { 68 if (args == null) { 69 res = (o.getArgs() == null) ? 0 : -1; 70 } else { 71 res = args.getClass().getName().compareTo(o.getArgs().getClass().getName()); 72 if (res == 0 && args.getClass().isArray()) { 73 res = Integer.valueOf(Array.getLength(args)).compareTo(Array.getLength(o.getArgs())); 74 for (int i = 0; res == 0 && i < Array.getLength(args); i++) { 75 final Object a1 = Array.get(args, i); 76 final Object a2 = Array.get(o.getArgs(), i); 77 if (a1 instanceof Comparable && a2 instanceof Comparable) { 78 res = ((Comparable) a1).compareTo(a2); 79 } 80 } 81 } else if (args instanceof Comparable) { 82 res = ((Comparable) args).compareTo(o.getArgs()); 83 } 84 } 85 } 86 87 if (CachedResultId.logger.isLoggable(Level.FINEST)) { 88 CachedResultId.logger.exiting("CachedResultId", "compareTo(CachedResultId=" + o + ")", 89 "end - return value=" + res); 90 } 91 return res; 92 } 93 94 /** 95 * @see java.lang.Object#equals(java.lang.Object) 96 */ 97 @Override 98 public boolean equals(final Object o) { 99 if (CachedResultId.logger.isLoggable(Level.FINEST)) { 100 CachedResultId.logger.entering("CachedResultId", "equals(Object=" + o + ")", "start"); 101 } 102 103 final boolean returnboolean = (o instanceof CachedResultId && compareTo((CachedResultId) o) == 0); 104 105 if (CachedResultId.logger.isLoggable(Level.FINEST)) { 106 CachedResultId.logger.exiting("CachedResultId", "equals(Object=" + o + ")", "end - return value=" 107 + returnboolean); 108 } 109 return returnboolean; 110 } 111 112 /** 113 * @return Returns the args. 114 */ 115 public Object getArgs() { 116 return args; 117 } 118 119 /** 120 * @return Returns the method. 121 */ 122 public String getMethod() { 123 return method; 124 } 125 126 /** 127 * @see java.lang.Object#hashCode() 128 */ 129 @Override 130 public int hashCode() { 131 return (method == null ? 0 : method.hashCode()) + (args == null ? 0 : args.hashCode()); 132 } 133 134 /** 135 * @param args 136 * The args to set. 137 */ 138 public void setArgs(final Object args) { 139 this.args = args; 140 } 141 142 /** 143 * @param method 144 * The method to set. 145 */ 146 public void setMethod(final String method) { 147 this.method = method; 148 } 149 150 /** 151 * @see java.lang.Object#toString() 152 */ 153 @Override 154 public String toString() { 155 return "CachedResultId( " + ((method == null) ? "null" : method) + ", " 156 + ((args == null) ? "null" : args.toString()) + ")"; 157 } 158 }