org.elasticsearch.common.lucene.BytesRefs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Elasticsearch subproject :server
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.common.lucene;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.BytesRefBuilder;
public class BytesRefs {
/**
* Converts a value to a string, taking special care if its a {@link BytesRef} to call
* {@link org.apache.lucene.util.BytesRef#utf8ToString()}.
*/
public static String toString(Object value) {
if (value == null) {
return null;
}
if (value instanceof BytesRef) {
return ((BytesRef) value).utf8ToString();
}
return value.toString();
}
/**
* Converts an object value to BytesRef.
*/
public static BytesRef toBytesRef(Object value) {
if (value == null) {
return null;
}
if (value instanceof BytesRef) {
return (BytesRef) value;
}
return new BytesRef(value.toString());
}
public static BytesRef toBytesRef(Object value, BytesRefBuilder spare) {
if (value == null) {
return null;
}
if (value instanceof BytesRef) {
return (BytesRef) value;
}
spare.copyChars(value.toString());
return spare.get();
}
}