1   /*
2    * Copyright 2002-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.json.sample;
18  
19  import net.sf.ezmorph.object.AbstractObjectMorpher;
20  
21  /**
22   * @author Andres Almiray <aalmiray@users.sourceforge.net>
23   */
24  public class IdBean {
25     private Id id;
26  
27     public Id getId() {
28        return id;
29     }
30  
31     public void setId( Id id ) {
32        this.id = id;
33     }
34  
35     public static class Id {
36        private long value;
37  
38        public Id() {
39           value = 0;
40        }
41  
42        public Id( long value ) {
43           super();
44           this.value = value;
45        }
46  
47        public boolean equals( Object obj ) {
48           if( obj == this ){
49              return true;
50           }
51           if( obj == null ){
52              return false;
53           }
54           if( !(obj instanceof Id) ){
55              return false;
56           }
57           Id other = (Id) obj;
58           return value == other.value;
59        }
60  
61        public long getValue() {
62           return value;
63        }
64  
65        public int hashCode() {
66           return getClass().hashCode() + (int) value;
67        }
68  
69        public void setValue( long value ) {
70           this.value = value;
71        }
72     }
73  
74     public static class IdMorpher extends AbstractObjectMorpher {
75        public Object morph( Object value ) {
76           if( value != null ){
77              if( value instanceof Number ){
78                 return new IdBean.Id( ((Number) value).longValue() );
79              }else if( value instanceof String ){
80                 return new IdBean.Id( new Long( (String) value ).longValue() );
81              }
82           }
83           return null;
84        }
85  
86        public Class morphsTo() {
87           return IdBean.Id.class;
88        }
89     }
90  }