-
Notifications
You must be signed in to change notification settings - Fork 215
/
ReferencePlaceholder.java
156 lines (129 loc) · 5.93 KB
/
ReferencePlaceholder.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.policies.enforcement.placeholders.references;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.placeholders.PlaceholderReferenceNotSupportedException;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.things.model.ThingId;
/**
* Responsible to extract and hold information about a referenced entity from a placeholder String.
*/
@Immutable
public final class ReferencePlaceholder {
private static String placeholderBeginning = "\\{\\{\\s?";
private static String placeholderEnding = "\\s?}}";
private static String referenceKeyword = "ref";
private static String everythingExceptFrontSlashesAndSpaces = "[^/\\s]";
private static String everythingExceptSpaces = "[^\\s]";
private static String entityTypeGroup = "(" + everythingExceptFrontSlashesAndSpaces + "+)";
private static String entityIdGroup = "(" + everythingExceptFrontSlashesAndSpaces + "+)";
private static String fieldSelectorGroup = "(" + everythingExceptSpaces + "+)";
private static Pattern referencePlaceholderPattern = Pattern.compile(
placeholderBeginning + referenceKeyword + ":" + entityTypeGroup + "/" + entityIdGroup + "/" +
fieldSelectorGroup + placeholderEnding);
private final ReferencedEntityType referencedEntityType;
private final EntityId referencedEntityId;
private final JsonPointer referencedField;
private ReferencePlaceholder(final ReferencedEntityType referencedEntityType, final EntityId referencedEntityId,
final JsonPointer referencedField) {
this.referencedEntityType = referencedEntityType;
this.referencedEntityId = referencedEntityId;
this.referencedField = referencedField;
}
/**
* Matches the given input against {@link #referencePlaceholderPattern}. If the input does not match the returned
* Optional will be empty. If it does match the Optional will contain an instance of {@code ReferencePlaceholder}.
*
* @param input The placeholder input that should be matched against {@link #referencePlaceholderPattern}.
* @return An Optional of {@code ReferencePlaceholder}. Optional is empty if the given input does not match
* {@link #referencePlaceholderPattern}.
*/
public static Optional<ReferencePlaceholder> fromCharSequence(@Nullable final CharSequence input) {
if (input == null) {
return Optional.empty();
}
final Matcher matcher = referencePlaceholderPattern.matcher(input);
if (matcher.find()) {
final ReferencedEntityType referencedEntityType = ReferencedEntityType.fromString(matcher.group(1));
final EntityId entityId = referencedEntityType.toEntityId(matcher.group(2));
final JsonPointer referencedField = JsonPointer.of(matcher.group(3));
return Optional.of(new ReferencePlaceholder(referencedEntityType, entityId, referencedField));
}
return Optional.empty();
}
ReferencedEntityType getReferencedEntityType() {
return referencedEntityType;
}
EntityId getReferencedEntityId() {
return referencedEntityId;
}
JsonPointer getReferencedField() {
return referencedField;
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ReferencePlaceholder that = (ReferencePlaceholder) o;
return referencedEntityType == that.referencedEntityType &&
Objects.equals(referencedEntityId, that.referencedEntityId) &&
Objects.equals(referencedField, that.referencedField);
}
@Override
public int hashCode() {
return Objects.hash(referencedEntityType, referencedEntityId, referencedField);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"referencedEntityType=" + referencedEntityType +
", referencedEntityId=" + referencedEntityId +
", referencedField=" + referencedField +
"]";
}
public enum ReferencedEntityType {
THINGS(ThingId::of);
private final Function<? super CharSequence, ? extends EntityId> entityIdFactory;
<I extends EntityId> ReferencedEntityType(final Function<? super CharSequence, I> entityIdFactory) {
this.entityIdFactory = entityIdFactory;
}
public static ReferencedEntityType fromString(final String referencedEntityTypeString) {
if ("things".equalsIgnoreCase(referencedEntityTypeString)) {
return THINGS;
} else {
final Set<CharSequence> supportedEntityTypes =
Arrays.stream(ReferencedEntityType.values()).map(Enum::name).collect(Collectors.toSet());
throw PlaceholderReferenceNotSupportedException
.fromUnsupportedEntityType(referencedEntityTypeString, supportedEntityTypes)
.build();
}
}
private EntityId toEntityId(final CharSequence entityId) {
return entityIdFactory.apply(entityId);
}
}
}