/
ContainerNaming.java
70 lines (62 loc) · 2.43 KB
/
ContainerNaming.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Copyright 2019 Immutables Authors and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.immutables.criteria.backend;
import com.google.common.base.CaseFormat;
import org.immutables.criteria.Criteria;
import java.util.Objects;
/**
* Resolves container name given a class. Container in this context means RDBMS table,
* mongo collection, ElasticSearch index etc.
*/
public interface ContainerNaming {
/**
* Resolve container name for a particular type. Container in this context means
* table, collection, index etc.
*
* @throws UnsupportedOperationException if name can't be derived
* @return name of the container (never null)
*/
String name(Class<?> type);
/**
* Resolve container name from {@link Criteria.Repository#name()} annotation.
*/
ContainerNaming FROM_REPOSITORY_ANNOTATION = entityClass -> {
Objects.requireNonNull(entityClass, "entityClass");
final Criteria.Repository annotation = entityClass.getAnnotation(Criteria.Repository.class);
if (annotation == null || annotation.name().isEmpty()) {
throw new UnsupportedOperationException(String.format("%s.name annotation is not defined on %s",
Criteria.Repository.class.getSimpleName(), entityClass.getName()));
}
return annotation.name();
};
/**
* Converts class simple name ({@link Class#getSimpleName()}) to camel format: {@code MyClass} into {@code myClass}
*/
ContainerNaming FROM_CLASSNAME = entityClass -> {
Objects.requireNonNull(entityClass, "entityClass");
return CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_CAMEL).convert(entityClass.getSimpleName());
};
/**
* Tries to derive name from annotation, if not possible derives it from name
*/
ContainerNaming DEFAULT = context -> {
try {
return FROM_REPOSITORY_ANNOTATION.name(context);
} catch (UnsupportedOperationException ignore) {
return FROM_CLASSNAME.name(context);
}
};
}