context/src/main/java/org/netbeans/html/context/impl/CtxImpl.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 04 Dec 2014 09:21:55 +0100
changeset 886 88d62267a0b5
parent 655 7211ec5f3172
permissions -rw-r--r--
#248918: Introducing technology identifiers
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;
jtulach@886
    47
import java.util.Comparator;
jaroslav@110
    48
import java.util.List;
jaroslav@110
    49
import net.java.html.BrwsrCtx;
jtulach@886
    50
import org.netbeans.html.context.spi.Contexts;
jaroslav@110
    51
jaroslav@110
    52
/** Implementation detail. Holds list of technologies for particular
jaroslav@110
    53
 * {@link BrwsrCtx}.
jaroslav@110
    54
 *
jtulach@655
    55
 * @author Jaroslav Tulach
jaroslav@110
    56
 */
jaroslav@110
    57
public final class CtxImpl {
jaroslav@110
    58
    private final List<Bind<?>> techs;
jtulach@886
    59
    private final Object[] context;
jaroslav@110
    60
    
jtulach@886
    61
    public CtxImpl(Object[] context) {
jtulach@886
    62
        this(context, new ArrayList<Bind<?>>());
jaroslav@110
    63
    }
jaroslav@110
    64
    
jtulach@886
    65
    private CtxImpl(Object[] context, List<Bind<?>> techs) {
jaroslav@110
    66
        this.techs = techs;
jtulach@886
    67
        this.context = context;
jaroslav@110
    68
    }
jaroslav@110
    69
    
jaroslav@110
    70
    public static <Tech> Tech find(BrwsrCtx context, Class<Tech> technology) {
jaroslav@110
    71
        CtxImpl impl = CtxAccssr.getDefault().find(context);
jaroslav@110
    72
        for (Bind<?> bind : impl.techs) {
jaroslav@110
    73
            if (technology == bind.clazz) {
jaroslav@110
    74
                return technology.cast(bind.impl);
jaroslav@110
    75
            }
jaroslav@110
    76
        }
jaroslav@110
    77
        return null;
jaroslav@110
    78
    }
jaroslav@110
    79
jaroslav@110
    80
    public BrwsrCtx build() {
jtulach@886
    81
        Collections.sort(techs, new BindCompare());
jaroslav@574
    82
        final List<Bind<?>> arr = Collections.unmodifiableList(techs);
jtulach@886
    83
        CtxImpl impl = new CtxImpl(context, arr);
jaroslav@574
    84
        BrwsrCtx ctx = CtxAccssr.getDefault().newContext(impl);
jaroslav@574
    85
        return ctx;
jaroslav@110
    86
    }
jaroslav@110
    87
jaroslav@110
    88
    public <Tech> void register(Class<Tech> type, Tech impl, int priority) {
jaroslav@110
    89
        techs.add(new Bind<Tech>(type, impl, priority));
jaroslav@110
    90
    }
jaroslav@110
    91
    
jtulach@886
    92
    private static final class Bind<Tech> {
jaroslav@110
    93
        private final Class<Tech> clazz;
jaroslav@110
    94
        private final Tech impl;
jaroslav@110
    95
        private final int priority;
jaroslav@110
    96
jaroslav@110
    97
        public Bind(Class<Tech> clazz, Tech impl, int priority) {
jaroslav@110
    98
            this.clazz = clazz;
jaroslav@110
    99
            this.impl = impl;
jaroslav@110
   100
            this.priority = priority;
jaroslav@110
   101
        }
jaroslav@110
   102
jaroslav@110
   103
        @Override
jaroslav@257
   104
        public String toString() {
jaroslav@257
   105
            return "Bind{" + "clazz=" + clazz + "@" + clazz.getClassLoader() + ", impl=" + impl + ", priority=" + priority + '}';
jaroslav@257
   106
        }
jaroslav@110
   107
    }
jtulach@886
   108
    
jtulach@886
   109
    private final class BindCompare implements Comparator<Bind<?>> {
jtulach@886
   110
        boolean isPrefered(Bind<?> b) {
jtulach@886
   111
            final Class<?> implClazz = b.impl.getClass();
jtulach@886
   112
            Contexts.Id id = implClazz.getAnnotation(Contexts.Id.class);
jtulach@886
   113
            if (id == null) {
jtulach@886
   114
                return false;
jtulach@886
   115
            }
jtulach@886
   116
            for (String v : id.value()) {
jtulach@886
   117
                for (Object c : context) {
jtulach@886
   118
                    if (v.equals(c)) {
jtulach@886
   119
                        return true;
jtulach@886
   120
                    }
jtulach@886
   121
                }
jtulach@886
   122
            }
jtulach@886
   123
            return false;
jtulach@886
   124
        }
jtulach@886
   125
        
jtulach@886
   126
        @Override
jtulach@886
   127
        public int compare(Bind<?> o1, Bind<?> o2) {
jtulach@886
   128
            boolean p1 = isPrefered(o1);
jtulach@886
   129
            boolean p2 = isPrefered(o2);
jtulach@886
   130
            if (p1 != p2) {
jtulach@886
   131
                return p1 ? -1 : 1;
jtulach@886
   132
            }
jtulach@886
   133
            if (o1.priority != o2.priority) {
jtulach@886
   134
                return o1.priority - o2.priority;
jtulach@886
   135
            }
jtulach@886
   136
            return o1.clazz.getName().compareTo(o2.clazz.getName());
jtulach@886
   137
        }
jtulach@886
   138
    } // end of BindCompare
jaroslav@110
   139
}