001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/fun/MemberHelper.java#5 $
003    // This software is subject to the terms of the Common Public License
004    // Agreement, available at the following URL:
005    // http://www.opensource.org/licenses/cpl.html.
006    // Copyright (C) 2003-2008 Julian Hyde and others
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009    */
010    package mondrian.olap.fun;
011    
012    import mondrian.olap.Member;
013    
014    /**
015     * This helper class is used by the Distinct(<set>) function to determine
016     * whether or not elements of different types within a set are equal.
017     */
018    public class MemberHelper {
019        private final Object mObject;
020        public MemberHelper(Object entry) {
021            if ((entry == null) ||
022                (entry instanceof Member) ||
023                (entry instanceof Member[])) {
024    
025                mObject = entry;
026            } else {
027                throw new IllegalArgumentException("Expected Member or Member[]");
028            }
029        }
030    
031        public boolean equals(Object obj) {
032            if ((obj == null) || !(obj instanceof MemberHelper)) {
033                return false;
034            } else if (this == obj) {
035                return true;
036            }
037    
038            MemberHelper mh = (MemberHelper)obj;
039    
040            if (this.mObject == null) {
041                return mh.mObject == null;
042            } else if (mh.mObject instanceof Member && mObject instanceof Member) {
043                return ((Member)mObject).equals(mh.mObject);
044            } else if (mh.mObject instanceof Member[] &&
045                mObject instanceof Member[]) {
046    
047                Member[] array1 = (Member[])mh.mObject;
048                Member[] array2 = (Member[])this.mObject;
049    
050                if (array1.length != array2.length) {
051                    return false;
052                }
053    
054                for (int idx = 0; idx < array1.length; idx++) {
055                    if (!array1[idx].equals(array2[idx])) {
056                        return false;
057                    }
058                }
059    
060                return true;
061            } else {
062                return false;
063            }
064        }
065    
066        public int hashCode() {
067            if (mObject instanceof Member) {
068                return mObject.hashCode();
069            } else if (mObject instanceof Member[]) {
070                int hash = 0;
071    
072                Member[] array = (Member[]) mObject;
073    
074                for (int idx = 0; idx < array.length; idx++) {
075                    hash ^= array[idx].hashCode();
076                }
077    
078                return hash;
079            } else {
080                return mObject.hashCode();
081            }
082        }
083    }
084    
085    // End MemberHelper.java