001    /**
002     * JDBM LICENSE v1.00
003     *
004     * Redistribution and use of this software and associated documentation
005     * ("Software"), with or without modification, are permitted provided
006     * that the following conditions are met:
007     *
008     * 1. Redistributions of source code must retain copyright
009     *    statements and notices.  Redistributions must also contain a
010     *    copy of this document.
011     *
012     * 2. Redistributions in binary form must reproduce the
013     *    above copyright notice, this list of conditions and the
014     *    following disclaimer in the documentation and/or other
015     *    materials provided with the distribution.
016     *
017     * 3. The name "JDBM" must not be used to endorse or promote
018     *    products derived from this Software without prior written
019     *    permission of Cees de Groot.  For written permission,
020     *    please contact cg@cdegroot.com.
021     *
022     * 4. Products derived from this Software may not be called "JDBM"
023     *    nor may "JDBM" appear in their names without prior written
024     *    permission of Cees de Groot.
025     *
026     * 5. Due credit should be given to the JDBM Project
027     *    (http://jdbm.sourceforge.net/).
028     *
029     * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
030     * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
031     * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
032     * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
033     * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
034     * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
035     * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
036     * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
037     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
038     * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
039     * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
040     * OF THE POSSIBILITY OF SUCH DAMAGE.
041     *
042     * Copyright 2001 (C) Alex Boisvert. All Rights Reserved.
043     * Contributions are Copyright (C) 2001 by their associated contributors.
044     *
045     */
046    
047    package jdbm.helper;
048    
049    import java.io.IOException;
050    import java.io.Serializable;
051    import java.util.Comparator;
052    
053    /**
054     * Comparator for objects which have been serialized into byte arrays.
055     * In effect, it wraps another Comparator which compares object and provides
056     * transparent deserialization from byte array to object.
057     *
058     * @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
059     * @version $Id: ObjectBAComparator.java,v 1.1 2002/05/31 06:33:20 boisvert Exp $
060     */
061    public final class ObjectBAComparator
062        implements Comparator, Serializable
063    {
064    
065        /**
066         * Version id for serialization.
067         */
068        final static long serialVersionUID = 1L;
069    
070    
071        /**
072         * Wrapped comparator.
073         */
074        private Comparator _comparator;
075    
076    
077        /**
078         * Construct an ObjectByteArrayComparator which wraps an Object Comparator.
079         *
080         * @param comparator Object comparator.
081         */
082        public ObjectBAComparator( Comparator comparator )
083        {
084            if ( comparator == null ) {
085                throw new IllegalArgumentException( "Argument 'comparator' is null" );
086            }
087    
088            _comparator = comparator;
089        }
090    
091    
092        /**
093         * Compare two objects.
094         *
095         * @param obj1 First object
096         * @param obj2 Second object
097         * @return 1 if obj1 > obj2, 0 if obj1 == obj2, -1 if obj1 < obj2
098         */
099         public int compare( Object obj1, Object obj2 )
100         {
101            if ( obj1 == null ) {
102                throw new IllegalArgumentException( "Argument 'obj1' is null" );
103            }
104    
105            if ( obj2 == null ) {
106                throw new IllegalArgumentException( "Argument 'obj2' is null" );
107            }
108    
109            try {
110                obj1 = Serialization.deserialize( (byte[]) obj1 );
111                obj2 = Serialization.deserialize( (byte[]) obj2 );
112    
113                return _comparator.compare( obj1, obj2 );
114            } catch ( IOException except ) {
115                throw new WrappedRuntimeException( except );
116            } catch ( ClassNotFoundException except ) {
117                throw new WrappedRuntimeException( except );
118            }
119         }
120    
121    
122        /**
123         * Compare two byte arrays.
124         */
125        public static int compareByteArray( byte[] thisKey, byte[] otherKey )
126        {
127            int len = Math.min( thisKey.length, otherKey.length );
128    
129            // compare the byte arrays
130            for ( int i=0; i<len; i++ ) {
131                if ( thisKey[i] >= 0 ) {
132                    if ( otherKey[i] >= 0 ) {
133                        // both positive
134                        if ( thisKey[i] < otherKey[i] ) {
135                            return -1;
136                        } else if ( thisKey[i] > otherKey[i] ) {
137                            return 1;
138                        }
139                    } else {
140                        // otherKey is negative => greater (because MSB is 1)
141                        return -1;
142                    }
143                } else {
144                    if ( otherKey[i] >= 0 ) {
145                        // thisKey is negative => greater (because MSB is 1)
146                        return 1;
147                    } else {
148                        // both negative
149                        if ( thisKey[i] < otherKey[i] ) {
150                            return -1;
151                        } else if ( thisKey[i] > otherKey[i] ) {
152                            return 1;
153                        }
154                    }
155                }
156            }
157            if ( thisKey.length == otherKey.length) {
158                return 0;
159            }
160            if ( thisKey.length < otherKey.length ) {
161                return -1;
162            }
163            return 1;
164        }
165    
166    }