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