KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > categories > Preorder


1 package JSci.maths.categories;
2
3 /**
4 * The Preorder class encapsulates preorders as categories.
5 * @version 1.0
6 * @author Mark Hale
7 */

8 public final class Preorder extends Object JavaDoc implements Category {
9         private final int N;
10         /**
11         * Constructs a preorder category.
12         */

13         public Preorder(int n) {
14                 N=n;
15         }
16         /**
17         * Returns the identity morphism for an object.
18         * @param a an Integer.
19         */

20         public Category.Morphism identity(Object JavaDoc a) {
21                 return new Relation((Integer JavaDoc)a, (Integer JavaDoc)a);
22         }
23         /**
24         * Returns the cardinality of an object.
25         * @param a an Integer.
26         * @return an Integer.
27         */

28         public Object JavaDoc cardinality(Object JavaDoc a) {
29                 return a;
30         }
31         /**
32         * Returns a hom-set.
33         */

34         public Category.HomSet hom(Object JavaDoc a,Object JavaDoc b) {
35                 final Integer JavaDoc i=(Integer JavaDoc)a;
36                 final Integer JavaDoc j=(Integer JavaDoc)b;
37                 if(i.compareTo(j)<=0)
38                         return new RelationSet(i,j);
39                 else
40                         return new RelationSet();
41         }
42         public Object JavaDoc initial() {
43                 return new Integer JavaDoc(0);
44         }
45         public Object JavaDoc terminal() {
46                 return new Integer JavaDoc(N-1);
47         }
48         /**
49         * Returns the ordinal that this category represents.
50         */

51         public int ordinal() {
52                 return N;
53         }
54         public class RelationSet implements Category.HomSet {
55                 public final Relation morphism;
56                 public RelationSet() {
57                         morphism=null;
58                 }
59                 public RelationSet(Integer JavaDoc a,Integer JavaDoc b) {
60                         morphism=new Relation(a,b);
61                 }
62         }
63         public class Relation implements Category.Morphism {
64                 private final Integer JavaDoc from,to;
65                 public Relation(Integer JavaDoc a,Integer JavaDoc b) {
66                         from=a;
67                         to=b;
68                 }
69                 public Object JavaDoc domain() {
70                         return from;
71                 }
72                 public Object JavaDoc codomain() {
73                         return to;
74                 }
75                 public Object JavaDoc map(Object JavaDoc o) {
76                         return to;
77                 }
78                 public Category.Morphism compose(Category.Morphism m) {
79                         if(m instanceof Relation) {
80                                 Relation r=(Relation)m;
81                                 if(to.equals(r.from))
82                                         return new Relation(from,r.to);
83                                 else
84                                         throw new UndefinedCompositionException();
85                         } else
86                                 throw new IllegalArgumentException JavaDoc("Morphism is not a Relation.");
87                 }
88         }
89 }
90
91
Popular Tags