2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.repo;
10 import static java.util.Objects.requireNonNull;
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.SchemaContextFactory;
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;
27 * A {@link SchemaRepository} which allows sharing of {@link SchemaContext} as long as their specification is the same.
30 * Note: for current implementation, "same" means the same filter and the same set of {@link SourceIdentifier}s.
33 @MetaInfServices(value = SchemaRepository.class)
34 public final class SharedSchemaRepository extends AbstractSchemaRepository implements Identifiable<String> {
35 private final LoadingCache<SchemaContextFactoryConfiguration, SchemaContextFactory> cacheByConfig = CacheBuilder
36 .newBuilder().softValues()
37 .build(new CacheLoader<SchemaContextFactoryConfiguration, SchemaContextFactory>() {
39 public SchemaContextFactory load(final SchemaContextFactoryConfiguration key) {
40 return new SharedSchemaContextFactory(SharedSchemaRepository.this, key);
44 private final @NonNull String id;
46 public SharedSchemaRepository(final String id) {
47 this.id = requireNonNull(id);
51 public @NonNull String getIdentifier() {
56 public @NonNull SchemaContextFactory createSchemaContextFactory(
57 final @NonNull SchemaContextFactoryConfiguration config) {
58 return cacheByConfig.getUnchecked(config);
62 public String toString() {
63 return "SchemaRepository: " + id;