001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: PrioritizedMethod.java 3668 2007-02-24 00:13:22Z gbevin $
007: */
008: package com.uwyn.rife.engine;
009:
010: import java.lang.reflect.Method;
011: import java.util.Arrays;
012:
013: class PrioritizedMethod implements Comparable {
014: private Method mMethod = null;
015: private int[] mPriority = null;
016:
017: PrioritizedMethod(Method method, int[] priority) {
018: assert method != null;
019:
020: mMethod = method;
021: mPriority = priority;
022: }
023:
024: public Method getMethod() {
025: return mMethod;
026: }
027:
028: public int[] getPriority() {
029: return mPriority;
030: }
031:
032: public int compareTo(Object other) {
033: if (null == other || !(other instanceof PrioritizedMethod)) {
034: return 0;
035: }
036:
037: PrioritizedMethod other_method = (PrioritizedMethod) other;
038: int[] other_priority = other_method.mPriority;
039: if (null == other_priority && mPriority != null) {
040: return 1;
041: } else if (null == mPriority && other_priority != null) {
042: return -1;
043: } else if (null == mPriority && null == other_priority) {
044: return mMethod.getName().compareTo(
045: other_method.getMethod().getName());
046: } else {
047: int position = 0;
048: while (position < mPriority.length
049: || position < other_priority.length) {
050: if (position >= mPriority.length
051: && position < other_priority.length) {
052: return -1;
053: } else if (position < mPriority.length
054: && position >= other_priority.length) {
055: return 1;
056: } else {
057: if (mPriority[position] < other_priority[position]) {
058: return -1;
059: } else if (mPriority[position] > other_priority[position]) {
060: return 1;
061: }
062: }
063: position++;
064: }
065:
066: return mMethod.getName().compareTo(
067: other_method.getMethod().getName());
068: }
069: }
070:
071: public boolean equals(Object other) {
072: if (null == other || !(other instanceof PrioritizedMethod)) {
073: return false;
074: }
075:
076: if (other == this ) {
077: return true;
078: }
079:
080: PrioritizedMethod other_method = (PrioritizedMethod) other;
081: int[] other_priority = other_method.mPriority;
082: if (null == other_priority && mPriority != null
083: || null == mPriority && other_priority != null) {
084: return false;
085: } else if (null == other_priority && null == mPriority) {
086: return mMethod.getName().equals(
087: other_method.getMethod().getName());
088: } else {
089: return Arrays.equals(mPriority, other_priority);
090: }
091: }
092:
093: public int hashCode() {
094: int result = 0;
095:
096: if (mMethod != null) {
097: result = mMethod.hashCode();
098: }
099:
100: if (mPriority != null) {
101: result = mPriority.hashCode();
102: }
103:
104: return result;
105: }
106: }
|