context/src/main/java/org/apidesign/html/context/spi/Contexts.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Thu, 06 Feb 2014 10:52:45 +0100
changeset 534 15907d1499fb
parent 365 5c93ad8c7a15
child 538 3459b847088f
permissions -rw-r--r--
Fixing Javadoc warnings
jaroslav@110
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@110
     3
 *
jaroslav@365
     4
 * Copyright 2013-2013 Oracle and/or its affiliates. All rights reserved.
jaroslav@110
     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.
jaroslav@110
     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@358
    30
 * Software is Oracle. Portions Copyright 2013-2013 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.
jaroslav@110
    42
 */
jaroslav@110
    43
package org.apidesign.html.context.spi;
jaroslav@110
    44
jaroslav@110
    45
import net.java.html.BrwsrCtx;
jaroslav@362
    46
import org.netbeans.html.context.impl.CtxImpl;
jaroslav@110
    47
jaroslav@110
    48
/**
jaroslav@110
    49
 *
jaroslav@110
    50
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@110
    51
 */
jaroslav@110
    52
public final class Contexts {
jaroslav@110
    53
    private Contexts() {
jaroslav@110
    54
    }
jaroslav@110
    55
jaroslav@110
    56
    /** Creates new, empty builder for creation of {@link BrwsrCtx}. At the
jaroslav@534
    57
     * end call the {@link Builder#build()} method to generate the context.
jaroslav@110
    58
     *
jaroslav@110
    59
     * @return new instance of the builder
jaroslav@110
    60
     */
jaroslav@110
    61
    public static Builder newBuilder() {
jaroslav@110
    62
        return new Builder();
jaroslav@110
    63
    }
jaroslav@110
    64
jaroslav@110
    65
    /** Seeks for the specified technology in the provided context.
jaroslav@110
    66
     * 
jaroslav@110
    67
     * @param <Tech> type of the technology
jaroslav@110
    68
     * @param context the context to seek in 
jaroslav@110
    69
     *    (previously filled with ({@link Builder#register(java.lang.Class, java.lang.Object, int)})
jaroslav@110
    70
     * @param technology class that identifies the technology
jaroslav@534
    71
     * @return the technology in the context or <code>null</code>
jaroslav@110
    72
     */
jaroslav@110
    73
    public static <Tech> Tech find(BrwsrCtx context, Class<Tech> technology) {
jaroslav@110
    74
        return CtxImpl.find(context, technology);
jaroslav@110
    75
    }
jaroslav@110
    76
jaroslav@110
    77
    /** Implementors of various HTML technologies should
jaroslav@534
    78
     * register their implementation via {@link java.util.ServiceProvider} so
jaroslav@534
    79
     * {@link java.util.ServiceProvider} can find them, when their JARs are included
jaroslav@110
    80
     * on the classpath of the running application.
jaroslav@110
    81
     *
jaroslav@110
    82
     * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@110
    83
     */
jaroslav@110
    84
    public static interface Provider {
jaroslav@110
    85
jaroslav@110
    86
        /** Register into the context if suitable technology is
jaroslav@110
    87
         * available for the requesting class.
jaroslav@110
    88
         * The provider should check if its own technology is available in current
jaroslav@110
    89
         * scope (e.g. proper JDK, proper browser, etc.). The provider
jaroslav@110
    90
         * can also find the right context depending on requestor's classloader, etc.
jaroslav@110
    91
         * <p>
jaroslav@110
    92
         * Providers should use {@link Builder} to enrich appropriately
jaroslav@110
    93
         * the context.
jaroslav@110
    94
         *
jaroslav@110
    95
         * @param context the context builder to fill with technologies
jaroslav@110
    96
         * @param requestor the application class requesting access the the HTML page
jaroslav@110
    97
         * @see BrwsrCtx#findDefault(java.lang.Class)
jaroslav@110
    98
         */
jaroslav@110
    99
        void fillContext(Builder context, Class<?> requestor);
jaroslav@110
   100
    }
jaroslav@110
   101
jaroslav@110
   102
    /** Support for providers of new {@link BrwsrCtx}. Providers of different
jaroslav@110
   103
     * technologies should be of particular interest in this class. End users
jaroslav@110
   104
     * designing their application with existing technologies should rather
jaroslav@110
   105
     * point their attention to {@link BrwsrCtx} and co.
jaroslav@110
   106
     *
jaroslav@110
   107
     * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@110
   108
     */
jaroslav@110
   109
    public static final class Builder {
jaroslav@256
   110
        private final CtxImpl impl = new CtxImpl();
jaroslav@110
   111
jaroslav@110
   112
        Builder() {
jaroslav@110
   113
        }
jaroslav@110
   114
        
jaroslav@110
   115
        /** Registers new technology into the context. Each technology is
jaroslav@110
   116
         * exactly identified by its implementation class and can be associated
jaroslav@110
   117
         * with (positive) priority. In case of presence of multiple technologies
jaroslav@110
   118
         * with the same class, the one with higher lower priority takes precedence.
jaroslav@256
   119
         * @param <Tech> type of technology to register
jaroslav@256
   120
         * @param type the real class of the technology type
jaroslav@256
   121
         * @param impl an instance of the technology class
jaroslav@256
   122
         * @param position the lower position, the more important implementation 
jaroslav@256
   123
         *    which will be consulted sooner when seeking for a {@link Contexts#find(net.java.html.BrwsrCtx, java.lang.Class)}
jaroslav@256
   124
         *    an implementation
jaroslav@256
   125
         * @return this builder
jaroslav@110
   126
         */
jaroslav@256
   127
        public <Tech> Builder register(Class<Tech> type, Tech impl, int position) {
jaroslav@256
   128
            if (position <= 0) {
jaroslav@110
   129
                throw new IllegalStateException();
jaroslav@110
   130
            }
jaroslav@256
   131
            this.impl.register(type, impl, position);
jaroslav@110
   132
            return this;
jaroslav@110
   133
        }
jaroslav@110
   134
jaroslav@110
   135
        /** Generates context based on values previously inserted into
jaroslav@110
   136
         * this builder.
jaroslav@110
   137
         *
jaroslav@110
   138
         * @return new, immutable instance of {@link BrwsrCtx}
jaroslav@110
   139
         */
jaroslav@110
   140
        public BrwsrCtx build() {
jaroslav@110
   141
            return impl.build();
jaroslav@110
   142
        }
jaroslav@110
   143
    }
jaroslav@110
   144
}