Repository: james-project Updated Branches: refs/heads/master d9978da2f -> 16eebbbb6
JAMES-2578 Add utils for Mailet new API Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/fc35485f Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/fc35485f Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/fc35485f Branch: refs/heads/master Commit: fc35485f9deb2638430f1af8a3a7bd24717058ef Parents: d9978da Author: Gautier DI FOLCO <[email protected]> Authored: Mon Nov 5 13:53:39 2018 +0100 Committer: Gautier DI FOLCO <[email protected]> Committed: Thu Nov 8 15:52:40 2018 +0100 ---------------------------------------------------------------------- .../java/org/apache/mailet/AttributeUtils.java | 67 ++++++++++++++++++++ 1 file changed, 67 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/fc35485f/mailet/api/src/main/java/org/apache/mailet/AttributeUtils.java ---------------------------------------------------------------------- diff --git a/mailet/api/src/main/java/org/apache/mailet/AttributeUtils.java b/mailet/api/src/main/java/org/apache/mailet/AttributeUtils.java new file mode 100644 index 0000000..c6b1965 --- /dev/null +++ b/mailet/api/src/main/java/org/apache/mailet/AttributeUtils.java @@ -0,0 +1,67 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you 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 org.apache.mailet; + +import java.util.Optional; + +/** + * Attribute + * + * @since Mailet API v3.2 + */ +public class AttributeUtils { + /** + * Returns the raw attribute value corresponding to an attribute name. + * + * A cast will be attempted to match the type expected by the user. + * + * Returns an empty optional upon missing attribute or type mismatch. + */ + public static <T> Optional<T> getValueAndCastFromMail(Mail mail, AttributeName name, Class<T> type) { + return getAttributeValueFromMail(mail, name) + .flatMap(value -> tryToCast(type, value)); + } + + /** + * Returns the raw attribute value corresponding to an attribute name. + * + * Returns an empty optional upon missing attribute + */ + public static Optional<?> getAttributeValueFromMail(Mail mail, AttributeName name) { + return mail + .getAttribute(name) + .map(AttributeUtils::getAttributeValue); + } + + /** + * Returns the raw value of an Attribute + */ + public static Object getAttributeValue(Attribute attribute) { + return attribute.getValue().getValue(); + } + + private static <T> Optional<T> tryToCast(Class<T> type, Object value) { + if (type.isInstance(value)) { + return Optional.of(type.cast(value)); + } else { + return Optional.empty(); + } + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
