context/src/main/java/org/netbeans/html/context/impl/CtxImpl.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Tue, 04 Mar 2014 17:32:54 +0100
branchBrwsrCtxExec
changeset 575 3e6c5f24b12d
parent 574 7958c9c205d9
child 655 7211ec5f3172
permissions -rw-r--r--
Deprecating Technology.runSafe - BrwsrCtx.execute is good enough
jaroslav@110
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@110
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 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@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.
jaroslav@110
    42
 */
jaroslav@362
    43
package org.netbeans.html.context.impl;
jaroslav@110
    44
jaroslav@110
    45
import java.util.ArrayList;
jaroslav@110
    46
import java.util.Collections;
jaroslav@110
    47
import java.util.List;
jaroslav@110
    48
import net.java.html.BrwsrCtx;
jaroslav@574
    49
import org.apidesign.html.context.spi.Contexts;
jaroslav@110
    50
jaroslav@110
    51
/** Implementation detail. Holds list of technologies for particular
jaroslav@110
    52
 * {@link BrwsrCtx}.
jaroslav@110
    53
 *
jaroslav@110
    54
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@110
    55
 */
jaroslav@110
    56
public final class CtxImpl {
jaroslav@110
    57
    private final List<Bind<?>> techs;
jaroslav@110
    58
    
jaroslav@110
    59
    public CtxImpl() {
jaroslav@110
    60
        techs = new ArrayList<Bind<?>>();
jaroslav@110
    61
    }
jaroslav@110
    62
    
jaroslav@110
    63
    private CtxImpl(List<Bind<?>> techs) {
jaroslav@110
    64
        this.techs = techs;
jaroslav@110
    65
    }
jaroslav@110
    66
    
jaroslav@110
    67
    public static <Tech> Tech find(BrwsrCtx context, Class<Tech> technology) {
jaroslav@110
    68
        CtxImpl impl = CtxAccssr.getDefault().find(context);
jaroslav@110
    69
        for (Bind<?> bind : impl.techs) {
jaroslav@110
    70
            if (technology == bind.clazz) {
jaroslav@110
    71
                return technology.cast(bind.impl);
jaroslav@110
    72
            }
jaroslav@110
    73
        }
jaroslav@110
    74
        return null;
jaroslav@110
    75
    }
jaroslav@110
    76
jaroslav@110
    77
    public BrwsrCtx build() {
jaroslav@110
    78
        Collections.sort(techs);
jaroslav@574
    79
        final List<Bind<?>> arr = Collections.unmodifiableList(techs);
jaroslav@574
    80
        CtxImpl impl = new CtxImpl(arr);
jaroslav@574
    81
        BrwsrCtx ctx = CtxAccssr.getDefault().newContext(impl);
jaroslav@574
    82
        return ctx;
jaroslav@110
    83
    }
jaroslav@110
    84
jaroslav@110
    85
    public <Tech> void register(Class<Tech> type, Tech impl, int priority) {
jaroslav@110
    86
        techs.add(new Bind<Tech>(type, impl, priority));
jaroslav@110
    87
    }
jaroslav@110
    88
    
jaroslav@110
    89
    private static final class Bind<Tech> implements Comparable<Bind<?>> {
jaroslav@110
    90
        private final Class<Tech> clazz;
jaroslav@110
    91
        private final Tech impl;
jaroslav@110
    92
        private final int priority;
jaroslav@110
    93
jaroslav@110
    94
        public Bind(Class<Tech> clazz, Tech impl, int priority) {
jaroslav@110
    95
            this.clazz = clazz;
jaroslav@110
    96
            this.impl = impl;
jaroslav@110
    97
            this.priority = priority;
jaroslav@110
    98
        }
jaroslav@110
    99
jaroslav@110
   100
        @Override
jaroslav@110
   101
        public int compareTo(Bind<?> o) {
jaroslav@110
   102
            if (priority != o.priority) {
jaroslav@110
   103
                return priority - o.priority;
jaroslav@110
   104
            }
jaroslav@110
   105
            return clazz.getName().compareTo(o.clazz.getName());
jaroslav@110
   106
        }
jaroslav@257
   107
jaroslav@257
   108
        @Override
jaroslav@257
   109
        public String toString() {
jaroslav@257
   110
            return "Bind{" + "clazz=" + clazz + "@" + clazz.getClassLoader() + ", impl=" + impl + ", priority=" + priority + '}';
jaroslav@257
   111
        }
jaroslav@110
   112
    }
jaroslav@110
   113
}