001package ca.uhn.fhir.rest.api;
002
003import ca.uhn.fhir.i18n.Msg;
004
005import java.io.Serializable;
006
007/*
008 * #%L
009 * HAPI FHIR - Core Library
010 * %%
011 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
012 * %%
013 * Licensed under the Apache License, Version 2.0 (the "License");
014 * you may not use this file except in compliance with the License.
015 * You may obtain a copy of the License at
016 *
017 *      http://www.apache.org/licenses/LICENSE-2.0
018 *
019 * Unless required by applicable law or agreed to in writing, software
020 * distributed under the License is distributed on an "AS IS" BASIS,
021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
022 * See the License for the specific language governing permissions and
023 * limitations under the License.
024 * #L%
025 */
026
027/**
028 * Represents values for <a href="http://hl7.org/implement/standards/fhir/search.html#sort">sorting</a> resources
029 * returned by a server.
030 */
031public class SortSpec implements Serializable {
032
033        private static final long serialVersionUID = 2866833099879713467L;
034        
035        private SortSpec myChain;
036        private String myParamName;
037        private SortOrderEnum myOrder;
038
039        /**
040         * Constructor
041         */
042        public SortSpec() {
043                super();
044        }
045
046        /**
047         * Constructor
048         * 
049         * @param theParamName
050         *            The search name to sort on. See {@link #setParamName(String)} for more information.
051         */
052        public SortSpec(String theParamName) {
053                super();
054                myParamName = theParamName;
055        }
056
057        /**
058         * Constructor
059         * 
060         * @param theParamName
061         *            The search name to sort on. See {@link #setParamName(String)} for more information.
062         * @param theOrder
063         *            The order, or <code>null</code>. See {@link #setOrder(SortOrderEnum)} for more information.
064         */
065        public SortSpec(String theParamName, SortOrderEnum theOrder) {
066                super();
067                myParamName = theParamName;
068                myOrder = theOrder;
069        }
070
071        /**
072         * Constructor
073         * 
074         * @param theParamName
075         *            The search name to sort on. See {@link #setParamName(String)} for more information.
076         * @param theOrder
077         *            The order, or <code>null</code>. See {@link #setOrder(SortOrderEnum)} for more information.
078         * @param theChain
079         *            The next sorting spec, to be applied only when this spec makes two entries equal. See
080         *            {@link #setChain(SortSpec)} for more information.
081         */
082        public SortSpec(String theParamName, SortOrderEnum theOrder, SortSpec theChain) {
083                super();
084                myParamName = theParamName;
085                myOrder = theOrder;
086                myChain = theChain;
087        }
088
089        /**
090         * Gets the chained sort specification, or <code>null</code> if none. If multiple sort parameters are chained
091         * (indicating a sub-sort), the second level sort is chained via this property.
092         */
093        public SortSpec getChain() {
094                return myChain;
095        }
096
097        /**
098         * Returns the actual name of the search param to sort by
099         */
100        public String getParamName() {
101                return myParamName;
102        }
103
104        /**
105         * Returns the sort order specified by this parameter, or <code>null</code> if none is explicitly provided (which
106         * means {@link SortOrderEnum#ASC} according to the <a
107         * href="http://hl7.org/implement/standards/fhir/search.html#sort">FHIR specification</a>)
108         */
109        public SortOrderEnum getOrder() {
110                return myOrder;
111        }
112
113        /**
114         * Sets the chained sort specification, or <code>null</code> if none. If multiple sort parameters are chained
115         * (indicating a sub-sort), the second level sort is chained via this property.
116         */
117        public SortSpec setChain(SortSpec theChain) {
118                if (theChain == this) {
119                        throw new IllegalArgumentException(Msg.code(1966) + "Can not chain this to itself");
120                }
121                myChain = theChain;
122                return this;
123        }
124
125        /**
126         * Sets the actual name of the search param to sort by
127         */
128        public SortSpec setParamName(String theFieldName) {
129                myParamName = theFieldName;
130                return this;
131        }
132
133        /**
134         * Sets the sort order specified by this parameter, or <code>null</code> if none should be explicitly defined (which
135         * means {@link SortOrderEnum#ASC} according to the <a
136         * href="http://hl7.org/implement/standards/fhir/search.html#sort">FHIR specification</a>)
137         */
138        public SortSpec setOrder(SortOrderEnum theOrder) {
139                myOrder = theOrder;
140                return this;
141        }
142
143}