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 com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
16 import org.opendaylight.yangtools.concepts.Identifiable;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
19 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
20 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
21 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
22 import org.opendaylight.yangtools.yang.model.repo.util.AbstractSchemaRepository;
25 * A {@link SchemaRepository} which allows sharing of {@link SchemaContext} as
26 * long as their specification is the same.
28 * Note: for current implementation, "same" means the same filter and the same
29 * set of {@link SourceIdentifier}s.
32 public final class SharedSchemaRepository extends AbstractSchemaRepository implements Identifiable<String> {
33 private final LoadingCache<SchemaSourceFilter, SchemaContextFactory> cache =
34 CacheBuilder.newBuilder().softValues().build(new CacheLoader<SchemaSourceFilter, SchemaContextFactory>() {
36 public SchemaContextFactory load(final SchemaSourceFilter key) {
37 return new SharedSchemaContextFactory(SharedSchemaRepository.this, key);
40 private final String id;
42 public SharedSchemaRepository(final String id) {
43 this.id = Preconditions.checkNotNull(id);
47 public String getIdentifier() {
52 public SchemaContextFactory createSchemaContextFactory(final SchemaSourceFilter filter) {
53 return cache.getUnchecked(filter);
57 public String toString() {
58 return "SchemaRepository: " + id;