b25cbeed8890c95b373069eb9de154ccab330c79
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / repo / SharedSchemaRepository.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.repo;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.cache.CacheBuilder;
14 import com.google.common.cache.CacheLoader;
15 import com.google.common.cache.LoadingCache;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.kohsuke.MetaInfServices;
18 import org.opendaylight.yangtools.concepts.Identifiable;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory;
21 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
23 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.repo.util.AbstractSchemaRepository;
25
26 /**
27  * A {@link SchemaRepository} which allows sharing of {@link SchemaContext} as long as their specification is the same.
28  *
29  * <p>
30  * Note: for current implementation, "same" means the same filter and the same set of {@link SourceIdentifier}s.
31  */
32 @Beta
33 @MetaInfServices(value = SchemaRepository.class)
34 public final class SharedSchemaRepository extends AbstractSchemaRepository implements Identifiable<String> {
35     private final LoadingCache<SchemaContextFactoryConfiguration, EffectiveModelContextFactory> cacheByConfig =
36             CacheBuilder.newBuilder().softValues()
37             .build(new CacheLoader<SchemaContextFactoryConfiguration, EffectiveModelContextFactory>() {
38                 @Override
39                 public EffectiveModelContextFactory load(final SchemaContextFactoryConfiguration key) {
40                     return new SharedSchemaContextFactory(SharedSchemaRepository.this, key);
41                 }
42             });
43
44     private final @NonNull String id;
45
46     public SharedSchemaRepository(final String id) {
47         this.id = requireNonNull(id);
48     }
49
50     @Override
51     public @NonNull String getIdentifier() {
52         return id;
53     }
54
55     @Override
56     public @NonNull EffectiveModelContextFactory createEffectiveModelContextFactory(
57             final @NonNull SchemaContextFactoryConfiguration config) {
58         return cacheByConfig.getUnchecked(config);
59     }
60
61     @Override
62     public String toString() {
63         return "SchemaRepository: " + id;
64     }
65 }