-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Examples.java
110 lines (102 loc) · 4.03 KB
/
Examples.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright 2001-present Stephen Colebourne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.joda.beans;
import org.joda.beans.gen.Address;
import org.joda.beans.gen.Documentation;
import org.joda.beans.gen.Person;
/**
* Examples using Person.
*
* @author Stephen Colebourne
*/
public class Examples {
public static void main(String[] args) {
// create the bean the hard way - could just do new Person() instead
Person p = Person.meta().builder().set("surname", "Smith").build();
// set surname using normal method
p.setSurname("Colebourne");
// query using property method
System.out.println(p.surname().get());
// query using meta-property method
System.out.println(Person.meta().surname().get(p));
// set/get forename using property method
p.forename().set("Stephen");
System.out.println(p.forename().get());
// set cars
p.numberOfCars().set(2);
// access all the properties
System.out.println(p.propertyNames());
System.out.println(JodaBeanUtils.flatten(p));
System.out.println(p);
// perform validation
boolean valid =
validateNotEmpty(p.surname()) &&
validateNotEmpty(p.forename());
System.out.println(valid ? "Valid" : "Not valid");
// extensions
p.getExtensions().set("suffix", "Jr");
System.out.println(p.propertyNames());
System.out.println(JodaBeanUtils.flatten(p));
System.out.println(p);
// create the bean the hard way - could just do new Address() instead
Address a = (Address) Address.meta().builder().build();
// set surname using normal method
a.setStreet("Barnsnap Close");
// query using property method
System.out.println(a.street().get());
// set/get forename using property method
a.city().set("Horsham");
System.out.println(a.city().get());
// set cars
a.number().set(22);
// access all the properties
System.out.println(a.propertyNames());
System.out.println(JodaBeanUtils.flatten(a));
System.out.println(a);
// perform validation
valid =
validateNotEmpty(a.street()) &&
validateNotEmpty(a.city());
System.out.println(valid ? "Valid" : "Not valid");
// generics
Documentation<Address> d = new Documentation<>();
d.setType("ADDRESS");
d.setContent(a);
Property<Address> dProp = d.content();
Address a2 = dProp.metaProperty().get(d);
System.out.println(a2);
Address a3 = Documentation.metaDocumentation(Address.class).content().get(d);
System.out.println(a3);
Documentation<Address> d2 = Documentation.metaDocumentation(Address.class).builder().build();
System.out.println(d2);
// try {
// Mongo mongo = new Mongo("127.0.0.1");
// System.out.println(mongo);
// mongo.dropDatabase("BeansTest");
// DB db = mongo.getDB("BeansTest");
// DBCollection coll = db.createCollection("Test", new BeanMongoDBObject(p));
// System.out.println(coll);
// System.out.println("Docs " + coll.getCount());
//
// } catch (Exception ex) {
// ex.printStackTrace();
// }
}
private static boolean validateNotEmpty(Property<String> property) {
String str = property.get();
return (str != null && str.length() > 0);
}
}