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

net.derquinse.common.uuid.UUIDs Maven / Gradle / Ivy

Go to download

Module containing support classes depending on Java SE 6, Guava 11 and Joda-Time 2.0

There is a newer version: 1.0.37
Show newest version
/*
 * Copyright (C) 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
 *
 *      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 net.derquinse.common.uuid;

import java.util.UUID;

import net.derquinse.common.base.NotInstantiable;

import com.google.common.base.Function;

/**
 * Static utility methods pertaining to {@code UUID} objects.
 * @author Andres Rodriguez
 */
public final class UUIDs extends NotInstantiable {
	/** Not instantiable. */
	private UUIDs() {
	}

	/**
	 * Returns a function transforming a string into an UUID. The returned function does not allow
	 * null inputs and throws {@link IllegalArgumentException} for strings not representing a valid
	 * UUID.
	 */
	public static Function fromString() {
		return FromString.INSTANCE;
	}

	private enum FromString implements Function {
		INSTANCE;

		/*
		 * (non-Javadoc)
		 * @see com.google.common.base.Function#apply(java.lang.Object)
		 */
		public UUID apply(String from) {
			return UUID.fromString(from);
		}

		@Override
		public String toString() {
			return "UUIDs.fromString";
		}
	}

	/**
	 * Returns an UUID parsed from a string.
	 * @param uuidString String to parse.
	 * @return The parsed UUID or {@code null} if the argument is {@code null} or unparseable.
	 * @see UUID#fromString(String).
	 */
	public static UUID safeFromString(String uuidString) {
		if (uuidString == null) {
			return null;
		}
		try {
			return UUID.fromString(uuidString);
		} catch (IllegalArgumentException e) {
			return null;
		}
	}

	/**
	 * Returns a function encapsulating the {@link #safeFromString(String) safeFromString} method.
	 * @return The {@link #safeFromString(String) safeFromString} function.
	 */
	public static Function safeFromString() {
		return SafeFromString.INSTANCE;
	}

	private enum SafeFromString implements Function {
		INSTANCE;

		/*
		 * (non-Javadoc)
		 * @see com.google.common.base.Function#apply(java.lang.Object)
		 */
		public UUID apply(String from) {
			return safeFromString(from);
		}

		@Override
		public String toString() {
			return "UUIDs.safeFromString";
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy