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