Cleanup use of Guava library
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / repo / util / SchemaSourceTransformer.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.model.repo.util;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.AsyncFunction;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import java.util.HashMap;
17 import java.util.Map;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
20 import org.opendaylight.yangtools.util.concurrent.ReflectiveExceptionMapper;
21 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
24 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
25 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
26 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
27 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
28 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
29 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
30
31 public class SchemaSourceTransformer<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
32         implements SchemaSourceListener, SchemaSourceProvider<D> {
33     private static final ExceptionMapper<SchemaSourceException> MAPPER = ReflectiveExceptionMapper.create(
34             "Source transformation", SchemaSourceException.class);
35
36     @FunctionalInterface
37     public interface Transformation<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
38             extends AsyncFunction<S, D> {
39         @Override
40         CheckedFuture<D, SchemaSourceException> apply(@Nonnull S input) throws Exception;
41     }
42
43     private final Map<PotentialSchemaSource<?>, RefcountedRegistration> sources = new HashMap<>();
44     private final SchemaSourceRegistry consumer;
45     private final SchemaRepository provider;
46     private final AsyncFunction<S, D> function;
47     private final Class<S> srcClass;
48     private final Class<D> dstClass;
49
50     public SchemaSourceTransformer(final SchemaRepository provider, final Class<S> srcClass,
51             final SchemaSourceRegistry consumer, final Class<D> dstClass, final AsyncFunction<S, D> function) {
52         this.provider = requireNonNull(provider);
53         this.consumer = requireNonNull(consumer);
54         this.function = requireNonNull(function);
55         this.srcClass = requireNonNull(srcClass);
56         this.dstClass = requireNonNull(dstClass);
57     }
58
59     @Override
60     public CheckedFuture<D, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
61         final CheckedFuture<S, SchemaSourceException> f = provider.getSchemaSource(sourceIdentifier, srcClass);
62         return Futures.makeChecked(Futures.transformAsync(f, function, MoreExecutors.directExecutor()), MAPPER);
63     }
64
65     @Override
66     public final void schemaSourceEncountered(final SchemaSourceRepresentation source) {
67         // Not interesting
68     }
69
70     @Override
71     public final void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
72         for (PotentialSchemaSource<?> src : sources) {
73             final Class<?> rep = src.getRepresentation();
74             if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
75                 registerSource(src);
76             }
77         }
78     }
79
80     @Override
81     public final void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
82         final Class<?> rep = source.getRepresentation();
83         if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
84             unregisterSource(source);
85         }
86     }
87
88     private void registerSource(final PotentialSchemaSource<?> src) {
89         RefcountedRegistration reg = sources.get(src);
90         if (reg != null) {
91             reg.incRef();
92             return;
93         }
94
95         final PotentialSchemaSource<D> newSrc = PotentialSchemaSource.create(src.getSourceIdentifier(), dstClass,
96                 src.getCost() + PotentialSchemaSource.Costs.COMPUTATION.getValue());
97
98         final SchemaSourceRegistration<D> r = consumer.registerSchemaSource(this, newSrc);
99         sources.put(src, new RefcountedRegistration(r));
100     }
101
102     private void unregisterSource(final PotentialSchemaSource<?> src) {
103         final RefcountedRegistration reg = sources.get(src);
104         if (reg != null && reg.decRef()) {
105             sources.remove(src);
106         }
107     }
108 }