• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alibaba / jetcache / #418

30 Aug 2024 10:16AM UTC coverage: 88.839% (+0.01%) from 88.825%
#418

push

web-flow
remove unused map in EmbededCache (#921)

4760 of 5358 relevant lines covered (88.84%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.21
/jetcache-core/src/main/java/com/alicp/jetcache/support/ObjectPool.java
1
package com.alicp.jetcache.support;
2

3
import org.slf4j.Logger;
4
import org.slf4j.LoggerFactory;
5

6
import java.util.concurrent.ArrayBlockingQueue;
7

8
/**
9
 * @Description
10
 * @author: zhangtong
11
 * @create: 2023/10/6 3:27 PM
12
 */
13
public class ObjectPool<T> {
14
        private final ArrayBlockingQueue<T> queue;
15
        private final int size;
16
        private final ObjectFactory<T> factory;
17
        private static final Logger logger = LoggerFactory.getLogger(ObjectPool.class);
1✔
18

19
        public ObjectPool(int size, ObjectFactory<T> factory) {
1✔
20
            this.size = size;
1✔
21
            this.factory = factory;
1✔
22
            queue = new ArrayBlockingQueue<>(size);
1✔
23
            for (int i = 0; i < size; i++) {
1✔
24
                queue.add(factory.create());
1✔
25
            }
26
            logger.debug("Init the object pool with size {}", size);
1✔
27
        }
1✔
28

29
        public T borrowObject() {
30
            T t = queue.poll();
1✔
31
            if(t == null) {
1✔
32
                logger.debug("The pool is not enough, create a new object");
×
33
                return factory.create();
×
34
            }
35
            return t;
1✔
36
        }
37

38
        public void returnObject(T obj) {
39
            if (obj == null) {
1✔
40
                return;
×
41
            }
42
            factory.reset(obj);
1✔
43
            queue.offer(obj);
1✔
44
        }
1✔
45

46
        public interface ObjectFactory<T> {
47
            T create();
48
            void reset(T obj);
49
        }
50
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc