spring-data-elasticsearch GeoShapeMappingParameters 源码

  • 2022-08-16
  • 浏览 (14)

spring-data-elasticsearch GeoShapeMappingParameters 代码

文件路径:/src/main/java/org/springframework/data/elasticsearch/core/index/GeoShapeMappingParameters.java

/*
 * Copyright 2020-2022 the original author or authors.
 *
 * 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
 *
 *      https://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.
 */
package org.springframework.data.elasticsearch.core.index;

import java.io.IOException;

import org.springframework.data.elasticsearch.annotations.GeoShapeField;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import com.fasterxml.jackson.databind.node.ObjectNode;

/**
 * @author Peter-Josef Meisch
 */
final class GeoShapeMappingParameters {
	private static final String FIELD_PARAM_TYPE = "type";
	private static final String FIELD_PARAM_COERCE = "coerce";
	private static final String FIELD_PARAM_IGNORE_MALFORMED = "ignore_malformed";
	private static final String FIELD_PARAM_IGNORE_Z_VALUE = "ignore_z_value";
	private static final String FIELD_PARAM_ORIENTATION = "orientation";

	private static final String TYPE_VALUE_GEO_SHAPE = "geo_shape";

	private final boolean coerce;
	private final boolean ignoreMalformed;
	private final boolean ignoreZValue;
	private final GeoShapeField.Orientation orientation;

	/**
	 * Creates a GeoShapeMappingParameters from the given annotation.
	 *
	 * @param annotation if null, default values are set in the returned object
	 * @return a parameters object
	 */
	public static GeoShapeMappingParameters from(@Nullable GeoShapeField annotation) {

		if (annotation == null) {
			return new GeoShapeMappingParameters(false, false, true, GeoShapeField.Orientation.ccw);
		} else {
			return new GeoShapeMappingParameters(annotation.coerce(), annotation.ignoreMalformed(), annotation.ignoreZValue(),
					annotation.orientation());
		}
	}

	private GeoShapeMappingParameters(boolean coerce, boolean ignoreMalformed, boolean ignoreZValue,
			GeoShapeField.Orientation orientation) {
		this.coerce = coerce;
		this.ignoreMalformed = ignoreMalformed;
		this.ignoreZValue = ignoreZValue;
		this.orientation = orientation;
	}

	public boolean isCoerce() {
		return coerce;
	}

	public boolean isIgnoreMalformed() {
		return ignoreMalformed;
	}

	public boolean isIgnoreZValue() {
		return ignoreZValue;
	}

	public GeoShapeField.Orientation getOrientation() {
		return orientation;
	}

	public void writeTypeAndParametersTo(ObjectNode objectNode) throws IOException {

		Assert.notNull(objectNode, "objectNode must not be null");

		if (coerce) {
			objectNode.put(FIELD_PARAM_COERCE, coerce);
		}

		if (ignoreMalformed) {
			objectNode.put(FIELD_PARAM_IGNORE_MALFORMED, ignoreMalformed);
		}

		if (!ignoreZValue) {
			objectNode.put(FIELD_PARAM_IGNORE_Z_VALUE, ignoreZValue);
		}

		if (orientation != GeoShapeField.Orientation.ccw) {
			objectNode.put(FIELD_PARAM_ORIENTATION, orientation.name());
		}

		objectNode.put(FIELD_PARAM_TYPE, TYPE_VALUE_GEO_SHAPE);
	}
}

相关信息

spring-data-elasticsearch 源码目录

相关文章

spring-data-elasticsearch AliasAction 源码

spring-data-elasticsearch AliasActionParameters 源码

spring-data-elasticsearch AliasActions 源码

spring-data-elasticsearch AliasData 源码

spring-data-elasticsearch DeleteTemplateRequest 源码

spring-data-elasticsearch ExistsTemplateRequest 源码

spring-data-elasticsearch GetTemplateRequest 源码

spring-data-elasticsearch MappingBuilder 源码

spring-data-elasticsearch MappingParameters 源码

spring-data-elasticsearch PutTemplateRequest 源码

0  赞