json/src/main/java/net/java/html/json/Model.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 02 Aug 2014 07:54:15 +0200
branchDeepWatch
changeset 788 f8ac4d547ad3
parent 655 7211ec5f3172
child 791 0923378f4424
permissions -rw-r--r--
Deep checking of @ComputedProperties is now default if one depends on another model class
jtulach@0
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@0
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jtulach@0
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jtulach@0
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jtulach@0
    42
 */
jtulach@0
    43
package net.java.html.json;
jtulach@0
    44
jtulach@0
    45
import java.lang.annotation.ElementType;
jtulach@0
    46
import java.lang.annotation.Retention;
jtulach@0
    47
import java.lang.annotation.RetentionPolicy;
jtulach@0
    48
import java.lang.annotation.Target;
jaroslav@232
    49
import java.net.URL;
jtulach@788
    50
import java.util.List;
jtulach@0
    51
jaroslav@232
    52
/** Defines a model class that represents a single 
jaroslav@232
    53
 * <a href="http://en.wikipedia.org/wiki/JSON">JSON</a>-like object
jaroslav@232
    54
 * named {@link #className()}. The generated class contains
jaroslav@232
    55
 * getters and setters for properties defined via {@link #properties()} and
jaroslav@232
    56
 * getters for other, derived properties defined by annotating methods
jaroslav@232
    57
 * of this class by {@link ComputedProperty}. Each property
jaroslav@232
    58
 * can be of primitive type, an {@link Enum enum type} or (in order to create 
jaroslav@232
    59
 * nested <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> structure)
jaroslav@232
    60
 * of another {@link Model class generated by @Model} annotation. Each property
jaroslav@232
    61
 * can either represent a single value or be an array of its values.
jtulach@0
    62
 * <p>
jaroslav@232
    63
 * The {@link #className() generated class}'s <code>toString</code> method
jtulach@0
    64
 * converts the state of the object into 
jaroslav@232
    65
 * <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> format. One can
jaroslav@232
    66
 * use {@link Models#parse(net.java.html.BrwsrCtx, java.lang.Class, java.io.InputStream)}
jaroslav@232
    67
 * method to read the JSON text stored in a file or other stream back into the Java model. 
jaroslav@232
    68
 * One can also use {@link OnReceive @OnReceive} annotation to read the model
jaroslav@232
    69
 * asynchronously from a {@link URL}.
jaroslav@32
    70
 * <p>
jaroslav@229
    71
 * An example where one defines class <code>Person</code> with four
jaroslav@229
    72
 * properties (<code>firstName</code>, <code>lastName</code>, array of <code>addresses</code> and
jaroslav@32
    73
 * <code>fullName</code>) follows:
jaroslav@32
    74
 * <pre>
jaroslav@32
    75
 * {@link Model @Model}(className="Person", properties={
jaroslav@32
    76
 *   {@link Property @Property}(name = "firstName", type=String.class),
jaroslav@32
    77
 *   {@link Property @Property}(name = "lastName", type=String.class)
jaroslav@229
    78
 *   {@link Property @Property}(name = "addresses", type=Address.class, array = true)
jaroslav@32
    79
 * })
jaroslav@229
    80
 * static class PersonModel {
jaroslav@32
    81
 *   {@link ComputedProperty @ComputedProperty}
jaroslav@32
    82
 *   static String fullName(String firstName, String lastName) {
jaroslav@32
    83
 *     return firstName + " " + lastName;
jaroslav@32
    84
 *   }
jaroslav@229
    85
 * 
jtulach@788
    86
 *   {@link ComputedProperty @ComputedProperty}
jtulach@788
    87
 *   static String mainAddress({@link List List&lt;Address&gt;} addresses) {
jtulach@788
    88
 *     for (Address a : addresses) {
jtulach@788
    89
 *       return a.getStreet() + " " + a.getTown();
jtulach@788
    90
 *     }
jtulach@788
    91
 *     return "No address";
jtulach@788
    92
 *   }
jtulach@788
    93
 * 
jaroslav@229
    94
 *   {@link Model @Model}(className="Address", properties={
jaroslav@229
    95
 *     {@link Property @Property}(name = "street", type=String.class),
jaroslav@229
    96
 *     {@link Property @Property}(name = "town", type=String.class)
jaroslav@229
    97
 *   })
jaroslav@229
    98
 *   static class AddressModel {
jaroslav@229
    99
 *   }
jaroslav@229
   100
 * }
jaroslav@229
   101
 * </pre>
jaroslav@232
   102
 * The generated model class has a default constructor, and also <em>quick
jaroslav@232
   103
 * instantiation</em> constructor that accepts all non-array properties 
jaroslav@232
   104
 * (in the order used in the {@link #properties()} attribute) and vararg list
jaroslav@232
   105
 * for the first array property (if any). One can thus use following code
jaroslav@232
   106
 * to create an instance of the Person and Address classes:
jaroslav@229
   107
 * <pre>
jaroslav@232
   108
 * 
jaroslav@232
   109
 * Person p = new Person("Jaroslav", "Tulach",
jaroslav@232
   110
 *   new Address("Markoušovice", "Úpice"),
jaroslav@232
   111
 *   new Address("V Parku", "Praha")
jaroslav@232
   112
 * );
jaroslav@232
   113
 * // p.toString() then returns equivalent of following <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> object
jaroslav@229
   114
 * {
jaroslav@229
   115
 *   "firstName" : "Jaroslav",
jaroslav@229
   116
 *   "lastName" : "Tulach",
jaroslav@229
   117
 *   "addresses" : [
jaroslav@232
   118
 *     { "street" : "Markoušovice", "town" : "Úpice" },
jaroslav@232
   119
 *     { "street" : "V Parku", "town" : "Praha" },
jaroslav@229
   120
 *   ]
jaroslav@32
   121
 * }
jaroslav@32
   122
 * </pre>
jaroslav@232
   123
 * <p>
jaroslav@232
   124
 * In case you are using <a href="http://knockoutjs.com/">Knockout technology</a>
jaroslav@232
   125
 * for Java then you can associate such model object with surrounding HTML page by
jaroslav@232
   126
 * calling: <code>p.applyBindings();</code>. The page can then use regular
jaroslav@232
   127
 * <a href="http://knockoutjs.com/">Knockout</a> bindings to reference your
jaroslav@232
   128
 * model and create dynamic connection between your model in Java and 
jaroslav@232
   129
 * live DOM structure in the browser:
jaroslav@232
   130
 * <pre>
jaroslav@232
   131
 * Name: &lt;span data-bind="text: fullName"&gt;
jaroslav@232
   132
 * &lt;div data-bind="foreach: addresses"&gt;
jaroslav@232
   133
 *   Lives in &lt;span data-bind="text: town"/&gt;
jaroslav@232
   134
 * &lt;/div&gt;
jaroslav@232
   135
 * </pre>
jaroslav@549
   136
 * <p>
jaroslav@549
   137
 * Visit an <a target="_blank" href="http://dew.apidesign.org/dew/#7510833">on-line demo</a>
jaroslav@549
   138
 * to see a histogram driven by the {@link Model} annotation or try 
jaroslav@549
   139
 * a little <a target="_blank" href="http://dew.apidesign.org/dew/#7263102">math test</a>.
jtulach@0
   140
 *
jtulach@655
   141
 * @author Jaroslav Tulach
jtulach@0
   142
 */
jtulach@0
   143
@Retention(RetentionPolicy.SOURCE)
jtulach@0
   144
@Target(ElementType.TYPE)
jtulach@0
   145
public @interface Model {
jtulach@0
   146
    /** Name of the model class */
jtulach@0
   147
    String className();
jtulach@0
   148
    /** List of properties in the model.
jtulach@0
   149
     */
jtulach@0
   150
    Property[] properties();
jtulach@0
   151
}