aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/language/provider/DefaultEncoderProvider.java
blob: 9b07ee55bd824a265a26851e7d0544047e8ea45a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.language.provider;

import com.google.inject.Inject;
import com.yahoo.container.di.componentgraph.Provider;
import com.yahoo.language.Language;
import com.yahoo.language.process.Encoder;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;

import java.util.List;

/**
 * Provides the default encoder implementation if no encoder component has been explicitly configured
 * (dependency injection will fallback to providers if no components of the requested type is found).
 *
 * @author bratseth
 */
@SuppressWarnings("unused") // Injected
public class DefaultEncoderProvider implements Provider<Encoder> {

    // Use lazy initialization to avoid expensive (memory-wise) instantiation
    private static final Encoder failingEncoder = new FailingEncoder();

    @Inject
    public DefaultEncoderProvider() { }

    @Override
    public Encoder get() { return failingEncoder; }

    @Override
    public void deconstruct() {}

    public static class FailingEncoder implements Encoder {

        @Override
        public List<Integer> encode(String text, Language language) {
            throw new IllegalStateException("No encoder has been configured");
        }

        @Override
        public Tensor encode(String text, Language language, TensorType tensorType) {
            throw new IllegalStateException("No encoder has been configured");
        }

    }

}