1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 import java.util.*;
26 
27 // LinkedHashMap implrementation
28 public class OrderedHashMap
29 {
30     private HashMap hm = new HashMap();
31     private LinkedList list  = new LinkedList();
32 
iterator()33     public Iterator    iterator()   { return list.iterator(); }
34 
isEmpty()35     public boolean isEmpty()        { return hm.isEmpty(); }
get( Object key )36     public Object get( Object key ) { return hm.get( key ); }
get( int index )37     public Object get( int index )  { return hm.get( list.get( index ) ); }
keys()38     public Iterator keys()       { return list.iterator(); }
add( Object key , Object value )39     public Object add( Object key , Object value )
40     {
41         list.add( key );
42         return hm.put( key, value );
43     }
add( int index , Object key , Object value )44     public Object add( int index , Object key , Object value )
45     {
46         list.add( index , key );
47         return hm.put( key, value );
48     }
remove( Object key )49     public Object remove( Object key )
50     {
51         list.remove( list.indexOf( key ) );
52         return hm.remove( key );
53     }
move( int idxFrom , int idxTo )54     public void move( int idxFrom , int idxTo )
55     {
56         Object key = list.get( idxFrom );
57         list.remove( idxFrom );
58         list.add( idxTo , key );
59     }
move( Object key , int idxTo )60     public void move( Object key , int idxTo )
61     {
62        move( list.indexOf( key ) , idxTo );
63     }
size()64     public int size()
65     {
66         return hm.size();
67     }
elements()68     public Enumeration elements()
69     {
70         return new OHMenum( this );
71     }
72 }
73 
74 final class OHMenum implements Enumeration
75 {
76     OrderedHashMap ohm;
77     int index = 0;
78 
OHMenum()79     private OHMenum(){};
OHMenum( OrderedHashMap ohm )80     public OHMenum( OrderedHashMap ohm ){
81         this.ohm = ohm ;
82     }
83 
hasMoreElements()84     public boolean hasMoreElements()
85     {
86         return index < ohm.size();
87     }
nextElement()88     public Object nextElement()
89     {
90         return ohm.get( index++ );
91     }
92 }
93