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

org.mycore.util.concurrent.MCRDecorator Maven / Gradle / Ivy

There is a newer version: 2024.05
Show newest version
/*
 * This file is part of ***  M y C o R e  ***
 * See http://www.mycore.de/ for details.
 *
 * MyCoRe is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MyCoRe is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MyCoRe.  If not, see .
 */

package org.mycore.util.concurrent;

import java.util.Optional;

import com.google.common.reflect.TypeToken;

/**
 * Classes can implement this interface if they want to use
 * the decorator pattern. Contains some static helper methods
 * to check if an instance is implementing the decorator.
 * 
 * @author Matthias Eichner
 */
public interface MCRDecorator {

    /**
     * Returns the enclosing instance.
     * 
     * @return the decorated instance
     */
    V get();

    /**
     * Checks if the given object is decorated by this interface.
     * 
     * @param decorator the interface to check
     * @return true if the instance is decorated by an MCRDecorator
     */
    static boolean isDecorated(Object decorator) {
        return TypeToken.of(decorator.getClass()).getTypes().interfaces().stream()
            .filter(tt -> tt.isSubtypeOf(MCRDecorator.class)).findAny().isPresent();
    }

    /**
     * Returns an optional with the enclosing value of the decorator. The decorator
     * should implement the {@link MCRDecorator} interface. If not, an empty optional
     * is returned.
     * 
     * 
    *
  • get: MCRDecorator -> MCRDecorator -> MCRDecorator -> object *
  • resolve: MCRDecorator -> MCRDecorator -> MCRDecorator -> object *
* * @param decorator the MCRDecorator * @return an optional with the decorated instance */ @SuppressWarnings("unchecked") static Optional get(Object decorator) { if (isDecorated(decorator)) { try { return Optional.of(((MCRDecorator) decorator).get()); } catch (Exception exc) { return Optional.empty(); } } return Optional.empty(); } /** * Same as {@link #get()}, but returns the last object which does not implement * the decorator interface anymore. * *
    *
  • get: MCRDecorator -> MCRDecorator -> MCRDecorator -> object *
  • resolve: MCRDecorator -> MCRDecorator -> MCRDecorator -> object *
* * @param decorator the MCRDecorator * @return an optional with the decorated instance */ static Optional resolve(Object decorator) { Optional base = get(decorator); while (base.isPresent()) { Optional nextLevel = get(base.get()); if (nextLevel.isPresent()) { base = nextLevel; } else { break; } } return base; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy