context/src/main/java/org/netbeans/html/context/impl/CtxImpl.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 365 5c93ad8c7a15
child 574 7958c9c205d9
permissions -rw-r--r--
Updating copyright headers to mention current year
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@110
    49
jaroslav@110
    50
/** Implementation detail. Holds list of technologies for particular
jaroslav@110
    51
 * {@link BrwsrCtx}.
jaroslav@110
    52
 *
jaroslav@110
    53
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@110
    54
 */
jaroslav@110
    55
public final class CtxImpl {
jaroslav@110
    56
    private final List<Bind<?>> techs;
jaroslav@110
    57
    
jaroslav@110
    58
    public CtxImpl() {
jaroslav@110
    59
        techs = new ArrayList<Bind<?>>();
jaroslav@110
    60
    }
jaroslav@110
    61
    
jaroslav@110
    62
    private CtxImpl(List<Bind<?>> techs) {
jaroslav@110
    63
        this.techs = techs;
jaroslav@110
    64
    }
jaroslav@110
    65
    
jaroslav@110
    66
    public static <Tech> Tech find(BrwsrCtx context, Class<Tech> technology) {
jaroslav@110
    67
        CtxImpl impl = CtxAccssr.getDefault().find(context);
jaroslav@110
    68
        for (Bind<?> bind : impl.techs) {
jaroslav@110
    69
            if (technology == bind.clazz) {
jaroslav@110
    70
                return technology.cast(bind.impl);
jaroslav@110
    71
            }
jaroslav@110
    72
        }
jaroslav@110
    73
        return null;
jaroslav@110
    74
    }
jaroslav@110
    75
jaroslav@110
    76
    public BrwsrCtx build() {
jaroslav@110
    77
        Collections.sort(techs);
jaroslav@110
    78
        CtxImpl impl = new CtxImpl(Collections.unmodifiableList(techs));
jaroslav@110
    79
        return CtxAccssr.getDefault().newContext(impl);
jaroslav@110
    80
    }
jaroslav@110
    81
jaroslav@110
    82
    public <Tech> void register(Class<Tech> type, Tech impl, int priority) {
jaroslav@110
    83
        techs.add(new Bind<Tech>(type, impl, priority));
jaroslav@110
    84
    }
jaroslav@110
    85
    
jaroslav@110
    86
    private static final class Bind<Tech> implements Comparable<Bind<?>> {
jaroslav@110
    87
        private final Class<Tech> clazz;
jaroslav@110
    88
        private final Tech impl;
jaroslav@110
    89
        private final int priority;
jaroslav@110
    90
jaroslav@110
    91
        public Bind(Class<Tech> clazz, Tech impl, int priority) {
jaroslav@110
    92
            this.clazz = clazz;
jaroslav@110
    93
            this.impl = impl;
jaroslav@110
    94
            this.priority = priority;
jaroslav@110
    95
        }
jaroslav@110
    96
jaroslav@110
    97
        @Override
jaroslav@110
    98
        public int compareTo(Bind<?> o) {
jaroslav@110
    99
            if (priority != o.priority) {
jaroslav@110
   100
                return priority - o.priority;
jaroslav@110
   101
            }
jaroslav@110
   102
            return clazz.getName().compareTo(o.clazz.getName());
jaroslav@110
   103
        }
jaroslav@257
   104
jaroslav@257
   105
        @Override
jaroslav@257
   106
        public String toString() {
jaroslav@257
   107
            return "Bind{" + "clazz=" + clazz + "@" + clazz.getClassLoader() + ", impl=" + impl + ", priority=" + priority + '}';
jaroslav@257
   108
        }
jaroslav@110
   109
    }
jaroslav@110
   110
}