From 16c1eb024d564cb6313ff7f97a937991c97882b2 Mon Sep 17 00:00:00 2001 From: emeroad Date: Fri, 13 Jan 2023 16:09:38 +0900 Subject: [PATCH] [#9633] Replace List with Map --- .../context/scope/DefaultTraceScopePool.java | 22 +++-- .../pinpoint/profiler/util/NameValueList.java | 87 ------------------- .../profiler/util/NameValueListTest.java | 74 ---------------- 3 files changed, 10 insertions(+), 173 deletions(-) delete mode 100644 profiler/src/main/java/com/navercorp/pinpoint/profiler/util/NameValueList.java delete mode 100644 profiler/src/test/java/com/navercorp/pinpoint/profiler/util/NameValueListTest.java diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/scope/DefaultTraceScopePool.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/scope/DefaultTraceScopePool.java index dcfd9c92d9af..d2175e7fe2f2 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/scope/DefaultTraceScopePool.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/scope/DefaultTraceScopePool.java @@ -16,33 +16,31 @@ package com.navercorp.pinpoint.profiler.context.scope; import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope; -import com.navercorp.pinpoint.profiler.util.NameValueList; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; /** * @author jaehong.kim */ public class DefaultTraceScopePool { - private final NameValueList list = new NameValueList<>(); + private final Map map = new HashMap<>(); public TraceScope get(String name) { - if (name == null) { - throw new IllegalArgumentException("name"); - } + Objects.requireNonNull(name, "name"); - return list.get(name); + return map.get(name); } public TraceScope add(String name) { - if (name == null) { - throw new IllegalArgumentException("name"); - } + Objects.requireNonNull(name, "name"); - final TraceScope oldScope = list.add(name, new DefaultTraceScope(name)); - return oldScope; + return map.put(name, new DefaultTraceScope(name)); } public void clear() { - list.clear(); + map.clear(); } } \ No newline at end of file diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/NameValueList.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/NameValueList.java deleted file mode 100644 index 88bfdf0ebf58..000000000000 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/NameValueList.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 2014 NAVER Corp. - * 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 com.navercorp.pinpoint.profiler.util; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author Jongho Moon - * - */ -public class NameValueList { - private final List> list; - - public NameValueList() { - this(0); - } - - public NameValueList(int size) { - this.list = new ArrayList<>(size); - } - - public T add(String name, T value) { - for (NameValue e : list) { - if (name.equals(e.name)) { - T old = e.value; - e.value = value; - - return old; - } - } - - list.add(new NameValue(name, value)); - return null; - } - - public T get(String name) { - for (NameValue e : list) { - if (name.equals(e.name)) { - return e.value; - } - } - - return null; - } - - public T remove(String name) { - int len = list.size(); - - for (int i = len - 1; i >= 0; i--) { - NameValue e = list.get(i); - - if (name.equals(e.name)) { - list.remove(i); - return e.value; - } - } - - return null; - } - - public void clear() { - list.clear(); - } - - private static final class NameValue { - private final String name; - private T value; - - public NameValue(String name, T value) { - this.name = name; - this.value = value; - } - } -} diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/util/NameValueListTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/util/NameValueListTest.java deleted file mode 100644 index 5051e7a958bb..000000000000 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/util/NameValueListTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2014 NAVER Corp. - * 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 com.navercorp.pinpoint.profiler.util; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -/** - * @author poap - */ -public class NameValueListTest { - private NameValueList list; - - @BeforeEach - public void beforeTest() { - list = new NameValueList<>(); - list.add("one", 1); - list.add("two", 2); - list.add("three", 3); - } - - @Test - public void add() { - Assertions.assertEquals(list.add("one", 11).intValue(), 1); - Assertions.assertEquals(list.add("two", 22).intValue(), 2); - Assertions.assertEquals(list.add("three", 33).intValue(), 3); - Assertions.assertNull(list.add("four", 4)); - Assertions.assertEquals(list.add("one", 111).intValue(), 11); - Assertions.assertEquals(list.add("two", 222).intValue(), 22); - Assertions.assertEquals(list.add("three", 333).intValue(), 33); - Assertions.assertEquals(list.add("four", 44).intValue(), 4); - Assertions.assertNull(list.add("five", 5)); - } - - @Test - public void get() { - Assertions.assertEquals(list.get("one").intValue(), 1); - Assertions.assertEquals(list.get("two").intValue(), 2); - Assertions.assertEquals(list.get("three").intValue(), 3); - Assertions.assertNull(list.get("four")); - } - - @Test - public void remove() { - Assertions.assertEquals(list.remove("one").intValue(), 1); - Assertions.assertEquals(list.remove("two").intValue(), 2); - Assertions.assertEquals(list.remove("three").intValue(), 3); - Assertions.assertNull(list.remove("four")); - Assertions.assertNull(list.remove("three")); - Assertions.assertNull(list.remove("two")); - Assertions.assertNull(list.remove("four")); - } - - @Test - public void clear() { - list.clear(); - Assertions.assertNull(list.get("one")); - Assertions.assertNull(list.get("two")); - Assertions.assertNull(list.get("three")); - } -}