остаются только те элементы, которые присутствовали и были удалены из fieldNames.
class MutableUserAgent extends UserAgentBaseListener implements UserAgent, Serializable, DefaultANTLRErrorListener {
private static final Logger LOG = LogManager.getLogger(UserAgent.class);
private static String getDefaultValueForField(String fieldName) {
if (fieldName.contains("NameVersion")) {
return UNKNOWN_NAME_VERSION;
}
if (fieldName.contains("Version")) {
return UNKNOWN_VERSION;
}
return UNKNOWN_VALUE;
}
private Set<String> wantedFieldNames = null;
private boolean hasSyntaxError;
private boolean hasAmbiguity;
private int ambiguityCount;
public void destroy() {
wantedFieldNames = null;
}
public boolean hasSyntaxError() {
return hasSyntaxError;
}
public boolean hasAmbiguity() {
return hasAmbiguity;
}
public int getAmbiguityCount() {
return ambiguityCount;
}
@Override
public void syntaxError(
Recognizer<?, ?> recognizer,
Object offendingSymbol,
int line,
int charPositionInLine,
String msg,
RecognitionException e) {
if (debug) {
LOG.error("Syntax error");
LOG.error("Source : {}", userAgentString);
LOG.error("Message: {}", msg);
}
hasSyntaxError = true;
MutableAgentField syntaxError = new MutableAgentField("false");
syntaxError.setValue("true", 1);
allFields.put(SYNTAX_ERROR, syntaxError);
}
@Override
public void reportAmbiguity(
Parser recognizer,
DFA dfa,
int startIndex,
int stopIndex,
boolean exact,
BitSet ambigAlts,
ATNConfigSet configs) {
hasAmbiguity = true;
ambiguityCount++;
}
// The original input value
private String userAgentString = null;
private boolean debug = false;
public boolean isDebug() {
return debug;
}
public void setDebug(boolean newDebug) {
this.debug = newDebug;
}
@Override
public boolean equals(Object o) {
return uaEquals(o);
}
@Override
public int hashCode() {
return uaHashCode();
}
private final Map<String, MutableAgentField> allFields = new HashMap<>();
private void setWantedFieldNames(Collection<String> newWantedFieldNames) {
if (newWantedFieldNames != null) {
if (!newWantedFieldNames.isEmpty()) {
wantedFieldNames = new LinkedHashSet<>(newWantedFieldNames);
}
}
}
public MutableUserAgent() {
}
public MutableUserAgent(Collection<String> wantedFieldNames) {
setWantedFieldNames(wantedFieldNames);
}
public MutableUserAgent(String userAgentString) {
// wantedFieldNames == null; --> Assume we want all fields.
setUserAgentString(userAgentString);
}
public MutableUserAgent(String userAgentString, Collection<String> wantedFieldNames) {
setWantedFieldNames(wantedFieldNames);
setUserAgentString(userAgentString);
}
public void setUserAgentString(String newUserAgentString) {
this.userAgentString = newUserAgentString;
reset();
}
public String getUserAgentString() {
return userAgentString;
}
public void reset() {
hasSyntaxError = false;
hasAmbiguity = false;
ambiguityCount = 0;
for (MutableAgentField field : allFields.values()) {
field.reset();
}
}
public static boolean isSystemField(String fieldname) {
switch (fieldname) {
case SET_ALL_FIELDS:
case SYNTAX_ERROR:
case USERAGENT_FIELDNAME:
return true;
default:
return false;
}
}
public void processSetAll() {
MutableAgentField setAllField = allFields.get(SET_ALL_FIELDS);
if (setAllField == null) {
return;
}
String value;
if (setAllField.isDefaultValue()) {
value = NULL_VALUE;
} else {
value = setAllField.getValue();
}
long confidence = setAllField.confidence;
for (Map.Entry<String, MutableAgentField> fieldEntry : allFields.entrySet()) {
if (!isSystemField(fieldEntry.getKey())) {
fieldEntry.getValue().setValue(value, confidence);
}
}
}
public void set(String attribute, String value, long confidence) {
MutableAgentField field = allFields.get(attribute);
if (field == null) {
field = new MutableAgentField(getDefaultValueForField(attribute));
}
boolean wasEmpty = confidence == -1;
boolean updated = field.setValue(value, confidence);
if (debug && !wasEmpty) {
if (updated) {
LOG.info("USE {} ({}) = {}", attribute, confidence, value);
} else {
LOG.info("SKIP {} ({}) = {}", attribute, confidence, value);
}
}
allFields.put(attribute, field);
}
public void setForced(String attribute, String value, long confidence) {
MutableAgentField field = allFields.get(attribute);
if (field == null) {
field = new MutableAgentField(getDefaultValueForField(attribute));
}
boolean wasEmpty = confidence == -1;
field.setValueForced(value, confidence);
if (debug && !wasEmpty) {
LOG.info("USE {} ({}) = {}", attribute, confidence, value);
}
allFields.put(attribute, field);
}
// The appliedMatcher parameter is needed for development and debugging.
public void set(MutableUserAgent newValuesUserAgent, Matcher appliedMatcher) { // NOSONAR: Unused parameter
for (String fieldName : newValuesUserAgent.allFields.keySet()) {
MutableAgentField field = newValuesUserAgent.allFields.get(fieldName);
set(fieldName, field.value, field.confidence);
}
}
void setImmediateForTesting(String fieldName, MutableAgentField agentField) {
allFields.put(fieldName, agentField);
}
public AgentField get(String fieldName) {
if (USERAGENT_FIELDNAME.equals(fieldName)) {
MutableAgentField agentField = new MutableAgentField(userAgentString);
agentField.setValue(userAgentString, 0L);
return agentField;
} else {
return allFields
.computeIfAbsent(
fieldName,
f -> new MutableAgentField(getDefaultValueForField(fieldName)));
}
}
public String getValue(String fieldName) {
if (USERAGENT_FIELDNAME.equals(fieldName)) {
return userAgentString;
}
AgentField field = allFields.get(fieldName);
if (field == null) {
return getDefaultValueForField(fieldName);
}
return field.getValue();
}
public Long getConfidence(String fieldName) {
if (USERAGENT_FIELDNAME.equals(fieldName)) {
return 0L;
}
AgentField field = allFields.get(fieldName);
if (field == null) {
return -1L;
}
return field.getConfidence();
}
@Override
public List<String> getAvailableFieldNamesSorted() {
List<String> fieldNames = new ArrayList<>(allFields.size() + 10);
if (wantedFieldNames == null) {
fieldNames.addAll(STANDARD_FIELDS);
allFields.forEach((fieldName, field) -> {
if (!fieldNames.contains(fieldName)) {
if (!field.isDefaultValue()) {
fieldNames.add(fieldName);
}
}
});
} else {
allFields.forEach((fieldName, field) -> {
if (!fieldNames.contains(fieldName)) {
if (!field.isDefaultValue()) {
if (wantedFieldNames.contains(fieldName)) {
fieldNames.add(fieldName);
}
}
}
});
}
// This is not a field; this is a special operator.
fieldNames.remove(SET_ALL_FIELDS);
List<String> result = PRE_SORTED_FIELDS_LIST
.stream()
.filter(fieldNames::remove)
.collect(Collectors.toList());
Collections.sort(fieldNames);
result.addAll(fieldNames);
return result;
}
@Override
public String toString() {
return toString(getAvailableFieldNamesSorted());
}
}
List <string>
List <string>
, вот его определние List<String> fieldNames = new ArrayList<>(allFields.size() + 10); в данном случаи allFields - это Map