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

com.github.fge.jsonschema.processing.ProcessingCache Maven / Gradle / Ivy

/*
 * Copyright (c) 2013, Francis Galiegue 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * Lesser GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package com.github.fge.jsonschema.processing;

import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.exceptions.unchecked.ProcessingError;
import com.github.fge.jsonschema.exceptions.unchecked.ProcessorBuildError;
import com.github.fge.jsonschema.report.ProcessingMessage;
import com.google.common.base.Equivalence;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import net.jcip.annotations.ThreadSafe;

import java.util.concurrent.ExecutionException;

import static com.github.fge.jsonschema.messages.ProcessingErrors.*;
import static com.google.common.base.Equivalence.Wrapper;

/**
 * A processing result cache usable in {@link Processor}s
 *
 * 

This is a wrapper over Guava's {@link LoadingCache} to which you pass * two arguments:

* *
    *
  • an {@link Equivalence} on cache keys,
  • *
  • a {@link CacheLoader} to load keys.
  • *
* *

Note that the cache loader must throw a {@link ProcessingException} * or any subclass.

* *

You can of course use Guava's builtin equivalences if you don't wish (or * need) to write your own:

* *
    *
  • {@link Equivalence#equals()} for the classical equals/hashcode key * comparison,
  • *
  • {@link Equivalence#identity()} for a reference equality comparison * (useful if you use enums or classes as keys, for instance).
  • *
* *

Note that there is one major difference with Guava's caches: this * cache does not accept null keys.

* * @param the type of (wrapped) keys for this cache * @param the type of values retrieved by this cache * * @see LoadingCache * @see Equivalence * @see Wrapper */ @ThreadSafe public final class ProcessingCache { /** * The internal loading cache */ private final LoadingCache, V> cache; /** * The equivalence for keys */ private final Equivalence equivalence; /** * Constructor * * @param equivalence the equivalence * @param cacheLoader the cache loader function * @throws ProcessorBuildError either the equivalence or the cache loader * is null */ public ProcessingCache(final Equivalence equivalence, final CacheLoader, V> cacheLoader) { if (equivalence == null) throw new ProcessorBuildError(new ProcessingMessage() .message(NULL_EQUIVALENCE)); if (cacheLoader == null) throw new ProcessorBuildError(new ProcessingMessage() .message(NULL_LOADER)); this.equivalence = equivalence; cache = CacheBuilder.newBuilder().recordStats().build(cacheLoader); } /** * Get an entry from the cache * * @param key the key * @return the value * @throws ProcessingException error when computing the value * @throws ProcessingError key is null */ public V get(final K key) throws ProcessingException { if (key == null) throw new ProcessingError(new ProcessingMessage() .message(NULL_KEYS_FORBIDDEN)); try { return cache.get(equivalence.wrap(key)); } catch (ExecutionException e) { throw (ProcessingException) e.getCause(); } } /** * Get an entry from the cache, "ignoring" exceptions * *

Exceptions are not really ignored per se. You can use this method * instead of {@link #get(Object)} if you are sure that no exceptions can * be thrown.

* *

If an exception is thrown nevertheless, it will be an unchecked * exception.

* * @param key the key * @return the matching value * @throws ProcessingError key is null */ public V getUnchecked(final K key) { if (key == null) throw new ProcessingError(new ProcessingMessage() .message(NULL_KEYS_FORBIDDEN)); return cache.getUnchecked(equivalence.wrap(key)); } @Override public String toString() { return cache.stats().toString(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy