001package ca.uhn.fhir.model.primitive; 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 023import org.apache.commons.lang3.StringUtils; 024 025import ca.uhn.fhir.model.api.BasePrimitive; 026import ca.uhn.fhir.model.api.IQueryParameterType; 027import ca.uhn.fhir.model.api.annotation.DatatypeDef; 028import ca.uhn.fhir.model.api.annotation.SimpleSetter; 029import ca.uhn.fhir.rest.param.StringParam; 030 031@DatatypeDef(name = "string") 032public class StringDt extends BasePrimitive<String> implements IQueryParameterType { 033 034 /** 035 * Create a new String 036 */ 037 public StringDt() { 038 super(); 039 } 040 041 /** 042 * Create a new String 043 */ 044 @SimpleSetter 045 public StringDt(@SimpleSetter.Parameter(name = "theString") String theValue) { 046 setValue(theValue); 047 } 048 049 public String getValueNotNull() { 050 return StringUtils.defaultString(getValue()); 051 } 052 053 /** 054 * Returns the value of this string, or <code>null</code> 055 */ 056 @Override 057 public String toString() { 058 return getValue(); 059 } 060 061 @Override 062 public int hashCode() { 063 final int prime = 31; 064 int result = 1; 065 result = prime * result + ((getValue() == null) ? 0 : getValue().hashCode()); 066 return result; 067 } 068 069 @Override 070 public boolean equals(Object obj) { 071 if (this == obj) 072 return true; 073 if (obj == null) 074 return false; 075 if (getClass() != obj.getClass()) 076 return false; 077 StringDt other = (StringDt) obj; 078 if (getValue() == null) { 079 if (other.getValue() != null) 080 return false; 081 } else if (!getValue().equals(other.getValue())) 082 return false; 083 return true; 084 } 085 086 /** 087 * {@inheritDoc} 088 */ 089 @Override 090 public void setValueAsQueryToken(String theQualifier, String theValue) { 091 setValue(theValue); 092 } 093 094 /** 095 * {@inheritDoc} 096 */ 097 @Override 098 public String getValueAsQueryToken() { 099 return getValue(); 100 } 101 102 /** 103 * Returns <code>true</code> if this datatype has no extensions, and has either a <code>null</code> value or an empty ("") value. 104 */ 105 @Override 106 public boolean isEmpty() { 107 boolean retVal = super.isBaseEmpty() && StringUtils.isBlank(getValue()); 108 return retVal; 109 } 110 111 @Override 112 public String getQueryParameterQualifier() { 113 return null; 114 } 115 116 @Override 117 protected String parse(String theValue) { 118 return theValue; 119 } 120 121 @Override 122 protected String encode(String theValue) { 123 return theValue; 124 } 125 126 /** 127 * <b>Not supported!</b> 128 * 129 * @deprecated get/setMissing is not supported in StringDt. Use {@link StringParam} instead if you 130 * need this functionality 131 */ 132 @Deprecated 133 @Override 134 public Boolean getMissing() { 135 return null; 136 } 137 138 /** 139 * <b>Not supported!</b> 140 * 141 * @deprecated get/setMissing is not supported in StringDt. Use {@link StringParam} instead if you 142 * need this functionality 143 */ 144 @Deprecated 145 @Override 146 public void setMissing(Boolean theMissing) { 147 throw new UnsupportedOperationException("get/setMissing is not supported in StringDt. Use {@link StringParam} instead if you need this functionality"); 148 } 149 150}