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

org.jppf.classloader.CompositeResourceWrapper Maven / Gradle / Ivy

There is a newer version: 6.3-alpha
Show newest version
/*
 * JPPF.
 * Copyright (C) 2005-2015 JPPF Team.
 * http://www.jppf.org
 *
 * Licensed 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.jppf.classloader;

import java.util.*;
import java.util.concurrent.Future;


/**
 * Instances of this class are intended for grouping multiple class loading requests together.
 * @author Laurent Cohen
 * @author Martin JANDA
 * @exclude
 */
public class CompositeResourceWrapper extends JPPFResourceWrapper
{
  /**
   * Explicit serialVersionUID.
   */
  private static final long serialVersionUID = 1L;
  /**
   * Mapping of futures to corresponding resource requests.
   */
  private final transient Map> futureMap = new HashMap<>();

  /**
   *
   */
  public CompositeResourceWrapper()
  {
  }

  @SuppressWarnings("unchecked")
  @Override
  public JPPFResourceWrapper[] getResources()
  {
    synchronized (getMonitor()) {
      Set set = (Set) getData(ResourceIdentifier.RESOURCES_KEY);
      if ((set == null) || set.isEmpty()) return EMPTY_RESOURCE_WRAPPER_ARRAY;
      else return set.toArray(new JPPFResourceWrapper[set.size()]);
    }
  }

  /**
   * Add or replace request to this composite request.
   * @param resource the request to add or replace.
   */
  @SuppressWarnings("unchecked")
  public void addOrReplaceResource(final JPPFResourceWrapper resource) {
    synchronized (getMonitor()) {
      Set resources = (Set) getData(ResourceIdentifier.RESOURCES_KEY);
      if(resources == null) {
        resources = new HashSet<>();
        setData(ResourceIdentifier.RESOURCES_KEY, resources);
      } else resources.remove(resource);
      resources.add(resource);
    }
  }

  /**
   * Add a request to this composite request.
   * @param resource the request to add.
   * @return a future for getting the response at a later time.
   */
  public Future addResource(final JPPFResourceWrapper resource)
  {
    Future f = futureMap.get(resource);
    if (f == null)
    {
      addOrReplaceResource(resource);
      f = new ResourceFuture<>();
      futureMap.put(resource, f);
    }
    return f;
  }

  /**
   * Get the mapping of futures to corresponding resource requests.
   * @return a map of resource definitions to their corresponding future.
   */
  public Map> getFutureMap()
  {
    return futureMap;
  }

  @SuppressWarnings("unchecked")
  @Override
  public String toString()
  {
    StringBuilder sb = new StringBuilder();
    sb.append(getClass().getSimpleName()).append('[');
    synchronized (getMonitor()) {
      sb.append("resources=").append(getData(ResourceIdentifier.RESOURCES_KEY));
    }
    sb.append(']');
    return sb.toString();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy