001/*-
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.parser.path;
021
022import org.apache.commons.lang3.Validate;
023import org.apache.commons.lang3.builder.EqualsBuilder;
024import org.apache.commons.lang3.builder.HashCodeBuilder;
025
026public class EncodeContextPathElement {
027    private final String myName;
028    private final boolean myResource;
029
030    public EncodeContextPathElement(String theName, boolean theResource) {
031        Validate.notBlank(theName);
032        myName = theName;
033        myResource = theResource;
034    }
035
036
037    public boolean matches(EncodeContextPathElement theOther) {
038        if (myResource != theOther.isResource()) {
039            return false;
040        }
041        String otherName = theOther.getName();
042        if (myName.equals(otherName)) {
043            return true;
044        }
045        /*
046         * This is here to handle situations where a path like
047         *    Observation.valueQuantity has been specified as an include/exclude path,
048         * since we only know that path as
049         *    Observation.value
050         * until we get to actually looking at the values there.
051         */
052        if (myName.length() > otherName.length() && myName.startsWith(otherName)) {
053            char ch = myName.charAt(otherName.length());
054            if (Character.isUpperCase(ch)) {
055                return true;
056            }
057        }
058        return myName.equals("*") || otherName.equals("*");
059    }
060
061    @Override
062    public boolean equals(Object theO) {
063        if (this == theO) {
064            return true;
065        }
066
067        if (theO == null || getClass() != theO.getClass()) {
068            return false;
069        }
070
071        EncodeContextPathElement that = (EncodeContextPathElement) theO;
072
073        return new EqualsBuilder()
074            .append(myResource, that.myResource)
075            .append(myName, that.myName)
076            .isEquals();
077    }
078
079    @Override
080    public int hashCode() {
081        return new HashCodeBuilder(17, 37)
082            .append(myName)
083            .append(myResource)
084            .toHashCode();
085    }
086
087    @Override
088    public String toString() {
089        if (myResource) {
090            return myName + "(res)";
091        }
092        return myName;
093    }
094
095    public String getName() {
096        return myName;
097    }
098
099    public boolean isResource() {
100        return myResource;
101    }
102}