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

com.liferay.adaptive.media.image.internal.configuration.AMImageAttributeMapping Maven / Gradle / Ivy

/**
 * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */

package com.liferay.adaptive.media.image.internal.configuration;

import com.liferay.adaptive.media.AMAttribute;
import com.liferay.adaptive.media.image.processor.AMImageAttribute;
import com.liferay.adaptive.media.image.processor.AMImageProcessor;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * Gives convenient access to a set of media attributes. It offers a type-safe
 * interface to access attribute values, accepting only attributes of the
 * correct type (those for adaptive images), and returning values of the correct
 * type.
 *
 * @author Adolfo Pérez
 */
public class AMImageAttributeMapping {

	/**
	 * Returns an {@link AMImageAttributeMapping} that uses the map as the
	 * underlying attribute storage.
	 *
	 * @param  properties the map to get the properties from
	 * @return a non-null mapping that provides type-safe access to
	 *         an underlying map
	 */
	public static AMImageAttributeMapping fromProperties(
		Map properties) {

		if (properties == null) {
			throw new IllegalArgumentException("Properties map is null");
		}

		Map, Optional> optionals =
			new HashMap<>();

		optionals.put(
			AMAttribute.getConfigurationUuidAMAttribute(),
			_getValueOptional(
				properties, AMAttribute.getConfigurationUuidAMAttribute()));
		optionals.put(
			AMAttribute.getContentLengthAMAttribute(),
			_getValueOptional(
				properties, AMAttribute.getContentLengthAMAttribute()));
		optionals.put(
			AMAttribute.getContentTypeAMAttribute(),
			_getValueOptional(
				properties, AMAttribute.getContentTypeAMAttribute()));
		optionals.put(
			AMAttribute.getFileNameAMAttribute(),
			_getValueOptional(
				properties, AMAttribute.getFileNameAMAttribute()));
		optionals.put(
			AMImageAttribute.AM_IMAGE_ATTRIBUTE_HEIGHT,
			_getValueOptional(
				properties, AMImageAttribute.AM_IMAGE_ATTRIBUTE_HEIGHT));
		optionals.put(
			AMImageAttribute.AM_IMAGE_ATTRIBUTE_WIDTH,
			_getValueOptional(
				properties, AMImageAttribute.AM_IMAGE_ATTRIBUTE_WIDTH));

		return new AMImageAttributeMapping(optionals);
	}

	/**
	 * Returns an {@link Optional} instance that contains the value of the
	 * attribute (if any) in this mapping.
	 *
	 * @param  amAttribute a non null attribute
	 * @return a non-null optional that contains the
	 *         (non-null) value (if any)
	 */
	public  Optional getValueOptional(
		AMAttribute amAttribute) {

		if (amAttribute == null) {
			throw new IllegalArgumentException(
				"Adaptive media attribute is null");
		}

		return (Optional)_optionals.get(amAttribute);
	}

	protected AMImageAttributeMapping(
		Map, Optional> optionals) {

		_optionals = optionals;
	}

	private static  Optional _getValueOptional(
		Map properties,
		AMAttribute amAttribute) {

		String value = properties.get(amAttribute.getName());

		if (value == null) {
			return Optional.empty();
		}

		return Optional.of(amAttribute.convert(value));
	}

	private final Map, Optional> _optionals;

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy