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

at.molindo.esi4j.rebuild.scrutineer.ObjectIdAndVersion Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2010 Molindo GmbH
 *
 * 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.
 */
package at.molindo.esi4j.rebuild.scrutineer;

import javax.annotation.Nullable;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import at.molindo.scrutineer.AbstractIdAndVersion;
import at.molindo.scrutineer.IdAndVersion;

public class ObjectIdAndVersion extends AbstractIdAndVersion {

	private final Object _object;
	private final Comparable _id;

	public static boolean isIdSupported(final Class idClass) {
		return String.class.equals(idClass) || Number.class.isAssignableFrom(idClass);
	}

	public static boolean isIdSupported(final Object id) {
		return id instanceof Number || id instanceof String;
	}

	public static String toId(final Object id) {
		return toStringId(toRawId(id));
	}

	private static Object toRawId(Object id) {
		if (id instanceof Number) {
			// TODO not every number should be turned into Long
			id = ((Number) id).longValue();
		}
		if (id instanceof Long || id instanceof String) {
			return id;
		} else {
			throw new IllegalArgumentException("unexpected id: " + id);
		}
	}

	private static String toStringId(final Object rawId) {
		return rawId.toString();
	}

	/**
	 * used for id and version coming from current index
	 *
	 * @see MappedObjectIdAndVersionFactory
	 */
	public ObjectIdAndVersion(final Object id, final long version) {
		this(id, version, null);
	}

	/**
	 * @param id
	 * @param version
	 * @param object
	 *            may be null when coming from index
	 */
	public ObjectIdAndVersion(final Object id, final long version, @Nullable final Object object) {
		super(version);
		_object = object;
		_id = (Comparable) toRawId(id);
	}

	/**
	 * @return null only for currently indexed objects
	 */
	@Nullable
	public Object getObject() {
		return _object;
	}

	@Override
	public String getId() {
		return toStringId(_id);
	}

	public Object getRawId() {
		return _id;
	}

	@Override
	protected HashCodeBuilder appendId(final HashCodeBuilder appender) {
		return appender.append(_id);
	}

	@Override
	protected CompareToBuilder appendId(final CompareToBuilder appender, final IdAndVersion other) {
		return appender.append(_id, ((ObjectIdAndVersion) other)._id);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy