Pool container : Generic Collection « Generics « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Generics » Generic Collection 




Pool container
        

/*
 * Copyright 2008-2010 the T2 Project ant the Others.
 *
 * 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.t2framework.commons.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

public class Pool<T> {

  private static int DEFAULT_POOL_MAX = 10;

  private List<T> stock;

  private Set<T> havings;

  public static <T> Pool<T> create(T... stocks) {
    return new Pool<T>(Arrays.asList(stocks));
  }

  public static <T extends Cloneable> Pool<T> create(T origin) {
    return create(origin, DEFAULT_POOL_MAX);
  }

  public static <T extends Cloneable> Pool<T> create(T origin, int poolMax) {
    if (origin == null) {
      throw new IllegalArgumentException("origin: " + origin);
    }
    if (poolMax < 1) {
      throw new IllegalArgumentException("poolMax: " + poolMax);
    }
    List<T> copies = new ArrayList<T>();
    for (int i = 0; i < poolMax; i++) {
      try {
        @SuppressWarnings("unchecked")
        T copied = (Torigin.getClass().getMethod("clone").invoke(
            origin);
        copies.add(copied);
      catch (Exception e) {
        throw new IllegalStateException(e);
      }
    }
    return new Pool<T>(copies);
  }

  private Pool(List<T> stocks) {
    if (stocks == null || stocks.size() == 0) {
      throw new IllegalArgumentException("stocks: " + stocks);
    }
    this.stock = new CopyOnWriteArrayList<T>();
    this.havings = new CopyOnWriteArraySet<T>();
    for (T stockItem : stocks) {
      stock.add(stockItem);
      havings.add(stockItem);
    }
  }

  public T borrowItem() {
    try {
      while (true) {
        if (< stock.size()) {
          return stock.remove(0);
        }
        Thread.sleep(10);
      }
    catch (InterruptedException e) {
      throw new IllegalStateException(e);
    }
  }

  public void returnItem(T item) {
    if (havings.contains(item== false) {
      throw new IllegalStateException("item is not mine");
    }
    stock.add(item);
  }

}

   
    
    
    
    
    
    
    
  














Related examples in the same category
1.Creating a Type-Specific List
2.A list declared to hold objects of a type T can also hold objects that extend from T.
3.A value retrieved from a type-specific list does not need to be casted
4.Generic ArrayListGeneric ArrayList
5.Generic Data Structure
6.Unchecked Example
7.Generic StackGeneric Stack
8.Enum and GenericEnum and Generic
9.Generic HashMapGeneric HashMap
10.Foreach and generic data structureForeach and generic data structure
11.Pre generics example that uses a collection.Pre generics example that uses a collection.
12.Data structure and collections: Modern, generics version.Data structure and collections: Modern, generics version.
13.Java generic: Generics and arrays.
14.Collections and Data structure: the generic wayCollections and Data structure: the generic way
15.The GenStack Class
16.Create a typesafe copy of a raw set.
17.Create a typesafe copy of a raw list.
18.Create a typesafe copy of a raw map.
19.Create a typesafe filter of an unchecked iterator.
20.Create a typesafe view over an underlying raw set.
21.Create a typesafe view over an underlying raw map.
22.Create a typesafe filter of an unchecked enumeration.
23.Circular Object Buffer
24.A collection that do not hold stored elements in memory, but serialize it into a file
25.Generic Triple structure
26.Generic Pair
27.A generic bag of properties used to store properties that apply to a specific target.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.