src/java.xml/share/classes/com/sun/org/apache/xml/internal/utils/ObjectPool.java
author joehw
Wed, 18 Oct 2017 13:25:49 -0700
changeset 47359 e1a6c0168741
parent 47216 71c04702a3d5
child 48409 5ab69533994b
permissions -rw-r--r--
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked Reviewed-by: lancea, rriggs, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
45853
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44797
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
     3
 * @LastModified: Oct 2017
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     8
 * this work for additional information regarding copyright ownership.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    11
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 */
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    21
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.xml.internal.utils;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    24
import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import com.sun.org.apache.xml.internal.res.XMLErrorResources;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import com.sun.org.apache.xml.internal.res.XMLMessages;
45853
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44797
diff changeset
    27
import java.lang.reflect.InvocationTargetException;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    28
import java.util.ArrayList;
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    29
import java.util.List;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 * Pool of object of a given type to pick from to help memory usage
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 * @xsl.usage internal
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
public class ObjectPool implements java.io.Serializable
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
    static final long serialVersionUID = -8519013691660936643L;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
  /** Type of objects in this pool.
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
   *  @serial          */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    42
  private final Class<?> objectType;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
    44
  /** Stack of given objects this points to.
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
   *  @serial          */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    46
  private final List<Object> freeStack;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
   * Constructor ObjectPool
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
   * @param type Type of objects for this pool
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
   */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    53
  public ObjectPool(Class<?> type)
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
    objectType = type;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    56
    freeStack = new ArrayList<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
   * Constructor ObjectPool
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
   * @param className Fully qualified name of the type of objects for this pool.
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
  public ObjectPool(String className)
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
    try
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
    {
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
    68
      objectType = ObjectFactory.findProviderClass(className, true);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
    catch(ClassNotFoundException cnfe)
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
      throw new WrappedRuntimeException(cnfe);
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    }
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    74
    freeStack = new ArrayList<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
   * Constructor ObjectPool
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
   * @param type Type of objects for this pool
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
   * @param size Size of vector to allocate
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
   */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    85
  public ObjectPool(Class<?> type, int size)
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    objectType = type;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    88
    freeStack = new ArrayList<>(size);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
   * Constructor ObjectPool
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
  public ObjectPool()
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    objectType = null;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    98
    freeStack = new ArrayList<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
   * Get an instance of the given object in this pool if available
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
   * @return an instance of the given object if available or null
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
  public synchronized Object getInstanceIfFree()
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    // Check if the pool is empty.
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
    if (!freeStack.isEmpty())
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
      // Remove object from end of free pool.
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   115
      Object result = freeStack.remove(freeStack.size() - 1);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
      return result;
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
   * Get an instance of the given object in this pool
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
   * @return An instance of the given object
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
  public synchronized Object getInstance()
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
    // Check if the pool is empty.
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
    if (freeStack.isEmpty())
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
      // Create a new object if so.
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
      try
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
      {
45853
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44797
diff changeset
   138
        return objectType.getConstructor().newInstance();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
      }
45853
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44797
diff changeset
   140
      catch (InstantiationException | IllegalAccessException | SecurityException |
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44797
diff changeset
   141
              IllegalArgumentException | InvocationTargetException | NoSuchMethodException ex){}
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
      // Throw unchecked exception for error in pool configuration.
45853
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44797
diff changeset
   144
      throw new RuntimeException(XMLMessages.createXMLMessage(
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44797
diff changeset
   145
              XMLErrorResources.ER_EXCEPTION_CREATING_POOL, null));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
      // Remove object from end of free pool.
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   151
      Object result = freeStack.remove(freeStack.size() - 1);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
      return result;
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
   * Add an instance of the given object to the pool
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
   * @param obj Object to add.
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
  public synchronized void freeInstance(Object obj)
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
    // Make sure the object is of the correct type.
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
    // Remove safety.  -sb
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
    // if (objectType.isInstance(obj))
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
    // {
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   169
    freeStack.add(obj);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
    // }
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
    // else
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
    // {
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
    //  throw new IllegalArgumentException("argument type invalid for pool");
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
    // }
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
}