All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ca.uhn.fhir.rest.gclient.QuantityClientParam Maven / Gradle / Ivy

Go to download

Builds the hapi fhir base. Requires Common lang, commons-text, and guava be built first.

The newest version!
/*
 * #%L
 * HAPI FHIR - Core Library
 * %%
 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package ca.uhn.fhir.rest.gclient;

import ca.uhn.fhir.rest.gclient.NumberClientParam.IMatches;
import ca.uhn.fhir.rest.param.ParamPrefixEnum;

import static org.apache.commons.lang3.StringUtils.defaultString;

/**
 * Quantity parameter type for use in fluent client interfaces
 */
@SuppressWarnings("deprecation")
public class QuantityClientParam extends BaseClientParam implements IParam {

	private String myParamName;

	public QuantityClientParam(String theParamName) {
		myParamName = theParamName;
	}

	public IMatches approximately() {
		return new NumberClientParam.IMatches() {
			@Override
			public IAndUnits number(long theNumber) {
				return new AndUnits(ParamPrefixEnum.APPROXIMATE, Long.toString(theNumber));
			}

			@Override
			public IAndUnits number(String theNumber) {
				return new AndUnits(ParamPrefixEnum.APPROXIMATE, theNumber);
			}
		};
	}

	public IMatches exactly() {
		return new NumberClientParam.IMatches() {
			@Override
			public IAndUnits number(long theNumber) {
				return new AndUnits(null, Long.toString(theNumber));
			}

			@Override
			public IAndUnits number(String theNumber) {
				return new AndUnits(null, theNumber);
			}
		};
	}

	@Override
	public String getParamName() {
		return myParamName;
	}

	public IMatches greaterThan() {
		return new NumberClientParam.IMatches() {
			@Override
			public IAndUnits number(long theNumber) {
				return new AndUnits(ParamPrefixEnum.GREATERTHAN, Long.toString(theNumber));
			}

			@Override
			public IAndUnits number(String theNumber) {
				return new AndUnits(ParamPrefixEnum.GREATERTHAN, theNumber);
			}
		};
	}

	public IMatches greaterThanOrEquals() {
		return new NumberClientParam.IMatches() {
			@Override
			public IAndUnits number(long theNumber) {
				return new AndUnits(ParamPrefixEnum.GREATERTHAN_OR_EQUALS, Long.toString(theNumber));
			}

			@Override
			public IAndUnits number(String theNumber) {
				return new AndUnits(ParamPrefixEnum.GREATERTHAN_OR_EQUALS, theNumber);
			}
		};
	}

	public IMatches lessThan() {
		return new NumberClientParam.IMatches() {
			@Override
			public IAndUnits number(long theNumber) {
				return new AndUnits(ParamPrefixEnum.LESSTHAN, Long.toString(theNumber));
			}

			@Override
			public IAndUnits number(String theNumber) {
				return new AndUnits(ParamPrefixEnum.LESSTHAN, theNumber);
			}
		};
	}

	public IMatches lessThanOrEquals() {
		return new NumberClientParam.IMatches() {
			@Override
			public IAndUnits number(long theNumber) {
				return new AndUnits(ParamPrefixEnum.LESSTHAN_OR_EQUALS, Long.toString(theNumber));
			}

			@Override
			public IAndUnits number(String theNumber) {
				return new AndUnits(ParamPrefixEnum.LESSTHAN_OR_EQUALS, theNumber);
			}
		};
	}

	/**
	 * Use the given quantity prefix
	 *
	 * @param thePrefix The prefix, or null for no prefix
	 */
	public IMatches withPrefix(final ParamPrefixEnum thePrefix) {
		return new NumberClientParam.IMatches() {
			@Override
			public IAndUnits number(long theNumber) {
				return new AndUnits(thePrefix, Long.toString(theNumber));
			}

			@Override
			public IAndUnits number(String theNumber) {
				return new AndUnits(thePrefix, theNumber);
			}
		};
	}

	public interface IAndUnits {

		ICriterion andNoUnits();

		ICriterion andUnits(String theUnits);

		ICriterion andUnits(String theSystem, String theUnits);
	}

	private class AndUnits implements IAndUnits {

		private ParamPrefixEnum myPrefix;
		private String myValue;

		public AndUnits(ParamPrefixEnum thePrefix, String theNumber) {
			myPrefix = thePrefix;
			myValue = theNumber;
		}

		@Override
		public ICriterion andNoUnits() {
			return andUnits(null, null);
		}

		@Override
		public ICriterion andUnits(String theUnits) {
			return andUnits(null, theUnits);
		}

		@Override
		public ICriterion andUnits(String theSystem, String theUnits) {
			return new QuantityCriterion(
					getParamName(), myPrefix, myValue, defaultString(theSystem), defaultString(theUnits));
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy