001package ca.uhn.fhir.rest.api;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2016 University Health Network
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
023/**
024 * Represents values for <a href="http://hl7.org/implement/standards/fhir/search.html#sort">sorting</a> resources
025 * returned by a server.
026 */
027public class SortSpec {
028
029        private SortSpec myChain;
030        private String myParamName;
031        private SortOrderEnum myOrder;
032
033        /**
034         * Constructor
035         */
036        public SortSpec() {
037        }
038
039        /**
040         * Constructor
041         * 
042         * @param theParamName
043         *            The search name to sort on. See {@link #setParamName(String)} for more information.
044         */
045        public SortSpec(String theParamName) {
046                super();
047                myParamName = theParamName;
048        }
049
050        /**
051         * Constructor
052         * 
053         * @param theParamName
054         *            The search name to sort on. See {@link #setParamName(String)} for more information.
055         * @param theOrder
056         *            The order, or <code>null</code>. See {@link #setOrder(SortOrderEnum)} for more information.
057         */
058        public SortSpec(String theParamName, SortOrderEnum theOrder) {
059                super();
060                myParamName = theParamName;
061                myOrder = theOrder;
062        }
063
064        /**
065         * Constructor
066         * 
067         * @param theParamName
068         *            The search name to sort on. See {@link #setParamName(String)} for more information.
069         * @param theOrder
070         *            The order, or <code>null</code>. See {@link #setOrder(SortOrderEnum)} for more information.
071         * @param theChain
072         *            The next sorting spec, to be applied only when this spec makes two entries equal. See
073         *            {@link #setChain(SortSpec)} for more information.
074         */
075        public SortSpec(String theParamName, SortOrderEnum theOrder, SortSpec theChain) {
076                super();
077                myParamName = theParamName;
078                myOrder = theOrder;
079                myChain = theChain;
080        }
081
082        /**
083         * Gets the chained sort specification, or <code>null</code> if none. If multiple sort parameters are chained
084         * (indicating a sub-sort), the second level sort is chained via this property.
085         */
086        public SortSpec getChain() {
087                return myChain;
088        }
089
090        /**
091         * Returns the actual name of the search param to sort by
092         */
093        public String getParamName() {
094                return myParamName;
095        }
096
097        /**
098         * Returns the sort order specified by this parameter, or <code>null</code> if none is explicitly provided (which
099         * means {@link SortOrderEnum#ASC} according to the <a
100         * href="http://hl7.org/implement/standards/fhir/search.html#sort">FHIR specification</a>)
101         */
102        public SortOrderEnum getOrder() {
103                return myOrder;
104        }
105
106        /**
107         * Sets the chained sort specification, or <code>null</code> if none. If multiple sort parameters are chained
108         * (indicating a sub-sort), the second level sort is chained via this property.
109         */
110        public SortSpec setChain(SortSpec theChain) {
111                if (theChain == this) {
112                        throw new IllegalArgumentException("Can not chain this to itself");
113                }
114                myChain = theChain;
115                return this;
116        }
117
118        /**
119         * Sets the actual name of the search param to sort by
120         */
121        public SortSpec setParamName(String theFieldName) {
122                myParamName = theFieldName;
123                return this;
124        }
125
126        /**
127         * Sets the sort order specified by this parameter, or <code>null</code> if none should be explicitly defined (which
128         * means {@link SortOrderEnum#ASC} according to the <a
129         * href="http://hl7.org/implement/standards/fhir/search.html#sort">FHIR specification</a>)
130         */
131        public SortSpec setOrder(SortOrderEnum theOrder) {
132                myOrder = theOrder;
133                return this;
134        }
135
136}