This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new 1471fbb  Context API refactoring.
1471fbb is described below

commit 1471fbb868f99f75f4636d62e56a9c1223868f5a
Author: JamesBognar <[email protected]>
AuthorDate: Sat Sep 25 13:18:30 2021 -0400

    Context API refactoring.
---
 .../main/java/org/apache/juneau/BeanContext.java   |  9 +++
 .../java/org/apache/juneau/BeanContextBuilder.java | 14 +++-
 .../main/java/org/apache/juneau/utils/HashKey.java | 93 ++++++++++++++++++++++
 3 files changed, 115 insertions(+), 1 deletion(-)

diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContext.java 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContext.java
index 899434c..a9a9ba6 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContext.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContext.java
@@ -1075,6 +1075,15 @@ public class BeanContext extends Context {
 
        /**
         * Constructor.
+        * 
+        * @param builder The builder for this object.
+        */
+       public BeanContext(BeanContextBuilder builder) {
+               this(builder.getContextProperties());
+       }
+
+       /**
+        * Constructor.
         *
         * <p>
         * Typically only called from {@link ContextBuilder#build(Class)} 
method.
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContextBuilder.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContextBuilder.java
index fa83711..d2ed05f 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContextBuilder.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContextBuilder.java
@@ -20,11 +20,13 @@ import java.io.*;
 import java.lang.annotation.*;
 import java.lang.reflect.*;
 import java.util.*;
+import java.util.concurrent.*;
 
 import org.apache.juneau.annotation.*;
 import org.apache.juneau.http.header.*;
 import org.apache.juneau.internal.*;
 import org.apache.juneau.transform.*;
+import org.apache.juneau.utils.*;
 
 /**
  * Builder class for building instances of serializers, parsers, and bean 
contexts.
@@ -52,6 +54,8 @@ import org.apache.juneau.transform.*;
 @FluentSetters
 public class BeanContextBuilder extends ContextBuilder {
 
+       private static final ConcurrentHashMap<HashKey,BeanContext> CACHE = new 
ConcurrentHashMap<>();
+
        /**
         * Constructor.
         *
@@ -86,7 +90,15 @@ public class BeanContextBuilder extends ContextBuilder {
 
        @Override /* ContextBuilder */
        public BeanContext build() {
-               return build(BeanContext.class);
+               ContextProperties cp = getContextProperties();
+               cp = cp.subset(new String[]{"Context","BeanContext"});
+               HashKey key = HashKey.create().add(cp).build();
+               BeanContext bc = CACHE.get(key);
+               if (bc == null) {
+                       bc = new BeanContext(this);
+                       CACHE.putIfAbsent(key, bc);
+               }
+               return bc;
        }
 
        
//-----------------------------------------------------------------------------------------------------------------
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/HashKey.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/HashKey.java
new file mode 100644
index 0000000..33f1340
--- /dev/null
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/HashKey.java
@@ -0,0 +1,93 @@
+// 
***************************************************************************************************************************
+// * 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.juneau.utils;
+
+import java.util.*;
+
+/**
+ * Represents a list of objects used to compare objects for equality.
+ */
+public class HashKey {
+
+       
//-----------------------------------------------------------------------------------------------------------------
+       // Static
+       
//-----------------------------------------------------------------------------------------------------------------
+       
+       /**
+        * Static creator.
+        * 
+        * @return A new builder.
+        */
+       public static Builder create() {
+               return new Builder();
+       }
+
+       
//-----------------------------------------------------------------------------------------------------------------
+       // Builder
+       
//-----------------------------------------------------------------------------------------------------------------
+
+       /**
+        * The builder class for this object.
+        */
+       public static class Builder {
+               List<Object> list = new ArrayList<>();
+
+               /**
+                * Adds a list of objects to this builder.
+                * 
+                * @param values The values to add to this list.
+                * @return This object.
+                */
+               public Builder add(Object...values) {
+                       Collections.addAll(list, values);
+                       return this;
+               }
+
+               /**
+                * Creates the key.
+                * 
+                * @return The key.
+                */
+               public HashKey build() {
+                       return new HashKey(this);
+               }
+       }
+
+       
//-----------------------------------------------------------------------------------------------------------------
+       // Instance
+       
//-----------------------------------------------------------------------------------------------------------------
+
+       private final int hashCode;
+       private final Object[] array;
+
+       HashKey(Builder builder) {
+               hashCode = builder.list.hashCode();
+               array = builder.list.toArray(new Object[builder.list.size()]);
+       }
+
+       @Override
+       public int hashCode() {
+               return hashCode;
+       }
+
+       @Override
+       public boolean equals(Object o) {
+               HashKey x = (HashKey)o;
+               if (array.length != x.array.length)
+                       return false;
+               for (int i = 0; i < array.length; i++)
+                       if (! array[i].equals(x.array[i]))
+                               return false;
+               return true;
+       }
+}

Reply via email to