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