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 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.mahout.cf.taste.impl.common;
 
 
 import java.util.List;
A helper class for implementing org.apache.mahout.cf.taste.common.Refreshable. This object is typically included in an implementation org.apache.mahout.cf.taste.common.Refreshable to implement org.apache.mahout.cf.taste.common.Refreshable.refresh(java.util.Collection). 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;

  

Parameters:
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) {
       .add(refreshable);
     }
   }
 
   public void removeDependency(Refreshable refreshable) {
     if (refreshable != null) {
       .remove(refreshable);
     }
   }

  
Typically this is called in org.apache.mahout.cf.taste.common.Refreshable.refresh(java.util.Collection) and is the entire body of that method.
 
   @Override
   public void refresh(Collection<RefreshablealreadyRefreshed) {
     if (!.isLocked()) {
       .lock();
       try {
         alreadyRefreshed = buildRefreshed(alreadyRefreshed);
         for (Refreshable dependency : ) {
           maybeRefresh(alreadyRefresheddependency);
         }
         if ( != null) {
           try {
             .call();
           } catch (Exception e) {
             .warn("Unexpected exception while refreshing"e);
           }
         }
       } finally {
         .unlock();
       }
     }
   }

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

Parameters:
currentAlreadyRefreshed org.apache.mahout.cf.taste.common.Refreshables to refresh later on
Returns:
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 org.apache.mahout.cf.taste.common.Refreshable to the given collection of org.apache.mahout.cf.taste.common.Refreshables if it is not already there and immediately refreshes it.

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