diff --git a/app/app.iml b/app/app.iml index 28de31495..6f25e0eb2 100644 --- a/app/app.iml +++ b/app/app.iml @@ -87,7 +87,6 @@ - diff --git a/app/build.gradle b/app/build.gradle index 6fc47dd49..68d165b66 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -29,6 +29,5 @@ dependencies { compile 'com.android.support:recyclerview-v7:22.0.0' compile 'com.android.support:cardview-v7:22.0.0' compile 'com.github.japgolly.android:svg-android:2.0.6' - compile 'com.intellij:annotations:12.0' compile fileTree(dir: 'libs', include: ['*.jar']) } diff --git a/app/src/main/java/com/hippo/ehviewer/network/EhHttpHelper.java b/app/src/main/java/com/hippo/ehviewer/network/EhHttpHelper.java index 2625ee3c6..7be731735 100644 --- a/app/src/main/java/com/hippo/ehviewer/network/EhHttpHelper.java +++ b/app/src/main/java/com/hippo/ehviewer/network/EhHttpHelper.java @@ -15,13 +15,31 @@ package com.hippo.ehviewer.network; +import android.support.annotation.NonNull; + import com.hippo.network.HttpHelper; +import com.hippo.util.Pool; import java.net.HttpURLConnection; import java.net.URL; public class EhHttpHelper extends HttpHelper { + private static final Pool sPool = new Pool<>(10); + + public static @NonNull EhHttpHelper obtain() { + EhHttpHelper ehh = sPool.obtain(); + if (ehh == null) { + ehh = new EhHttpHelper(); + } + return ehh; + } + + public static void recycle(@NonNull EhHttpHelper ehh) { + ehh.reset(); + sPool.recycle(ehh); + } + @Override protected String getCookie(URL url) { return super.getCookie(url); diff --git a/app/src/main/java/com/hippo/util/Pool.java b/app/src/main/java/com/hippo/util/Pool.java new file mode 100644 index 000000000..1cd7bddb2 --- /dev/null +++ b/app/src/main/java/com/hippo/util/Pool.java @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2015 Hippo Seven + * + * 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 + * 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 com.hippo.util; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +public class Pool { + + private static final int MAX_POOL_SIZE = 50; + + private final Object mPoolSync = new Object(); + private int mPoolMaxSize; + private int mPoolSize = 0; + + private @Nullable Node mFirstNode = null; + private @Nullable Node mLastNode = null; + + public Pool() { + this(MAX_POOL_SIZE); + } + + public Pool(int size) { + if (size <= 0) { + throw new IllegalStateException("Pool max size must 1 or larger"); + } + mPoolMaxSize = size; + } + + /** + * @return null if no item in pool + */ + public @Nullable E obtain() { + synchronized (mPoolSync) { + if (mFirstNode != null && mFirstNode.value != null) { + Node node = mFirstNode; + mFirstNode = node.next; + if (mFirstNode != null) { + mFirstNode.pre = null; + } + E e = node.value; + recycleNode(node); + mPoolSize--; + return e; + } + } + return null; + } + + /** + * Add item to pool + * + * @param e the item + */ + public void recycle(@NonNull E e) { + synchronized (mPoolSync) { + if (mPoolSize < mPoolMaxSize) { + Node node = obtainEmptyNode(); + node.value = e; + node.next = mFirstNode; + if (mFirstNode != null) { + mFirstNode.pre = node; + } + mFirstNode = node; + mPoolSize++; + + if (mLastNode == null) { + mLastNode = node; + } + } + } + } + + private @NonNull Node obtainEmptyNode() { + if (mLastNode == null || mLastNode.value != null) { + // No empty node + return new Node<>(); + } else { + Node node = mLastNode; + mLastNode = node.pre; + + if (mLastNode == null) { + mFirstNode = null; + } else { + mLastNode.next = null; + } + + node.pre = null; + return node; + } + } + + private void recycleNode(@NonNull Node node) { + if (mLastNode == null) { + node.value = null; + node.pre = null; + node.next = null; + mFirstNode = node; + mLastNode = node; + } else { + node.value = null; + mLastNode.next = node; + node.pre = mLastNode; + node.next = null; + mLastNode = node; + } + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for(Node node = mFirstNode; node != null; node = node.next) { + sb.append(node.value).append(';'); + } + return sb.toString(); + } + + private static class Node { + public @Nullable V value; + public @Nullable Node pre; + public @Nullable Node next; + } +}