KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > Maps


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solutions Corp. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*
9  * Created on Nov 19, 2004
10  *
11  * Author Ben Yu
12  */

13 package jfun.parsec;
14
15
16
17 /**
18  * This class provides some standard Map implementations.
19  * @author Ben Yu
20  *
21  * Nov 19, 2004
22  */

23 public final class Maps {
24   private static final Unary<Object JavaDoc> _id = new Unary<Object JavaDoc>(){
25     public Object JavaDoc map(Object JavaDoc v) {
26       return v;
27     }
28     public String JavaDoc toString(){return "id";}
29   };
30   private static final Mapn<Object JavaDoc[]> _idn = new Mapn<Object JavaDoc[]>(){
31     public Object JavaDoc[] map(Object JavaDoc... v){
32       return v;
33     }
34   };
35   /**
36    * Returns an identity map. map a = a.
37    * @return the Map instance.
38    */

39   public static <x> Unary<x> id(){return (Unary<x>)_id;}
40   /**
41    * Returns an identity map. map a = a.
42    * @param type the class literal for the type parameter.
43    * @return the Map instance.
44    */

45   public static <R> Unary<R> id(Class JavaDoc<R> type){
46     return id();
47   }
48   /**
49    * Returns an identity map. map a = a.
50    * @return the Mapn instance.
51    */

52   public static Mapn<Object JavaDoc[]> idn(){return _idn;}
53   /**
54    * Creates a map that maps any object to the same object.
55    * @param v the object that is gonna be returned from the Map.
56    * @return the Map instance.
57    */

58   public static <x, V> Map<x, V> cnst(final V v){
59     return new Map<x,V>(){
60       public V map(final x from){return v;}
61     };
62   }
63   /**
64    * Adapts a java.util.Map to jfun.util.Map.
65    * @param m the java.util.Map object.
66    * @return the jfun.util.Map instance.
67    */

68   public static <From,To> Map<From,To> jmap(final java.util.Map JavaDoc<From, To> m){
69     return new Map<From,To>(){
70       public To map(final From k){
71         return m.get(k);
72       }
73     };
74   }
75   
76   /**
77    * Transform a Tok object to the wrapped token object.
78    * @return the Map instance.
79    */

80   public static <T> Map<Tok, T> toToken(){
81     return new Map<Tok, T>(){
82       public T map(final Tok o){
83         return (T)o.getToken();
84       }
85     };
86   }
87   private static final Map2 _id2 = new Map2(){
88     public Object JavaDoc map(Object JavaDoc o1, Object JavaDoc o2){
89       return new Pair(o1, o2);
90     }
91   };
92   private static final Map3 _id3 = new Map3(){
93     public Object JavaDoc map(Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3){
94       return new Tuple3(o1, o2, o3);
95     }
96   };
97   private static final Map4 _id4 = new Map4(){
98     public Object JavaDoc map(Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3, Object JavaDoc o4){
99       return new Tuple4(o1, o2, o3, o4);
100     }
101   };
102   private static final Map5 _id5 = new Map5(){
103     public Object JavaDoc map(Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3, Object JavaDoc o4, Object JavaDoc o5){
104       return new Tuple5(o1, o2, o3, o4, o5);
105     }
106   };
107   /**
108    * Create a Map2 object that stores the two objects into a Pair object.
109    */

110   public static <A,B> Map2<A,B,Pair<A,B>> id2(){
111     return _id2;
112   }
113   /**
114    * Create a Map3 object that stores the 3 objects into a Tuple3 object.
115    */

116   public static <A,B,C> Map3<A,B,C,Tuple3<A,B,C>> id3(){
117     return _id3;
118   }
119   /**
120    * Create a Map4 object that stores the 4 objects into a Tuple4 object.
121    */

122   public static <A,B,C,D> Map4<A,B,C,D,Tuple4<A,B,C,D>> id4(){
123     return _id4;
124   }
125   /**
126    * Create a Map5 object that stores the 5 objects into a Tuple5 object.
127    */

128   public static <A,B,C,D,E> Map5<A,B,C,D,E,Tuple5<A,B,C,D,E>> id5(){
129     return _id5;
130   }
131 }
132
Popular Tags