Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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 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.
 import java.util.List;
A helper class for implementing This object is typically included in an implementation to implement It execute the class's own supplied update logic, after updating all the object's dependencies. This also ensures that dependencies are not updated multiple times.
 public final class RefreshHelper implements Refreshable {
   private static final Logger log = LoggerFactory.getLogger(RefreshHelper.class);
   private final List<Refreshabledependencies;
   private final ReentrantLock refreshLock;
   private final Callable<?> refreshRunnable;


refreshRunnable encapsulates the containing object's own refresh logic
   public RefreshHelper(Callable<?> refreshRunnable) {
     this. = new ArrayList<Refreshable>(3);
     this. = new ReentrantLock();
     this. = refreshRunnable;

Add a dependency to be refreshed first when the encapsulating object does.
   public void addDependency(Refreshable refreshable) {
     if (refreshable != null) {
   public void removeDependency(Refreshable refreshable) {
     if (refreshable != null) {

Typically this is called in and is the entire body of that method.
   public void refresh(Collection<RefreshablealreadyRefreshed) {
     if (!.isLocked()) {
       try {
         alreadyRefreshed = buildRefreshed(alreadyRefreshed);
         for (Refreshable dependency : ) {
         if ( != null) {
           try {
           } catch (Exception e) {
             .warn("Unexpected exception while refreshing"e);
       } finally {

Creates a new and empty java.util.Collection if the method parameter is null.

currentAlreadyRefreshed to refresh later on
an empty java.util.Collection if the method param was null or the unmodified method param.
   public static Collection<RefreshablebuildRefreshed(Collection<RefreshablecurrentAlreadyRefreshed) {
     return currentAlreadyRefreshed == null ? new HashSet<Refreshable>(3) : currentAlreadyRefreshed;

Adds the specified to the given collection of if it is not already there and immediately refreshes it.

alreadyRefreshed the collection of
refreshable the to potentially add and refresh
  public static void maybeRefresh(Collection<RefreshablealreadyRefreshedRefreshable refreshable) {
    if (!alreadyRefreshed.contains(refreshable)) {
      .info("Added refreshable: {}"refreshable);
      .info("Refreshed: {}"alreadyRefreshed);
New to GrepCode? Check out our FAQ X