mirror of https://github.com/longtai-cn/hippo4j
organize the toolkit, add references and reference sources (#835)
* fix : organize the toolkit, add references and reference sources * fix : add MemoryUtil for get memory info * fix : code format adjustmentpull/840/head
parent
8e397741d1
commit
8eaf587f6a
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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 cn.hippo4j.common.toolkit;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.MemoryMXBean;
|
||||
import java.lang.management.MemoryUsage;
|
||||
|
||||
/**
|
||||
* memory util<br>
|
||||
* the obtained information is not invalid, after a long wait, obtain it again
|
||||
*
|
||||
* @author liuwenhao
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class MemoryUtil {
|
||||
|
||||
static MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
|
||||
static MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
|
||||
static MemoryUsage noHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
|
||||
|
||||
/**
|
||||
* get used memory in heap
|
||||
*
|
||||
* @return long bytes
|
||||
*/
|
||||
public static long heapMemoryUsed() {
|
||||
return heapMemoryUsage.getUsed();
|
||||
}
|
||||
|
||||
/**
|
||||
* get max memory in heap
|
||||
*
|
||||
* @return long bytes
|
||||
*/
|
||||
public static long heapMemoryMax() {
|
||||
return heapMemoryUsage.getMax();
|
||||
}
|
||||
|
||||
/**
|
||||
* get free memory in heap
|
||||
*
|
||||
* @return long bytes
|
||||
*/
|
||||
public static long heapMemoryFree() {
|
||||
return Math.subtractExact(heapMemoryMax(), heapMemoryUsed());
|
||||
}
|
||||
|
||||
/**
|
||||
* get used memory in no-heap
|
||||
*
|
||||
* @return long bytes
|
||||
*/
|
||||
public static long noHeapMemoryUsed() {
|
||||
return noHeapMemoryUsage.getUsed();
|
||||
}
|
||||
|
||||
/**
|
||||
* get max memory in no-heap
|
||||
*
|
||||
* @return long bytes
|
||||
*/
|
||||
public static long noHeapMemoryMax() {
|
||||
return noHeapMemoryUsage.getMax();
|
||||
}
|
||||
|
||||
/**
|
||||
* get free memory in no-heap
|
||||
*
|
||||
* @return long bytes
|
||||
*/
|
||||
public static long noHeapMemoryFree() {
|
||||
return Math.subtractExact(noHeapMemoryMax(), noHeapMemoryUsed());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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 cn.hippo4j.common.toolkit;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MemoryUtilTest {
|
||||
|
||||
@Test
|
||||
public void heapMemoryUsed() {
|
||||
long memoryUsed = MemoryUtil.heapMemoryUsed();
|
||||
Assert.assertNotEquals(0, memoryUsed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void heapMemoryMax() {
|
||||
long memoryUsed = MemoryUtil.heapMemoryMax();
|
||||
Assert.assertNotEquals(0, memoryUsed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void heapMemoryFree() {
|
||||
long memoryUsed = MemoryUtil.heapMemoryFree();
|
||||
Assert.assertNotEquals(0, memoryUsed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noHeapMemoryUsed() {
|
||||
long memoryUsed = MemoryUtil.noHeapMemoryUsed();
|
||||
Assert.assertNotEquals(0, memoryUsed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noHeapMemoryMax() {
|
||||
long memoryUsed = MemoryUtil.noHeapMemoryMax();
|
||||
Assert.assertNotEquals(0, memoryUsed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noHeapMemoryFree() {
|
||||
long memoryUsed = MemoryUtil.noHeapMemoryFree();
|
||||
Assert.assertNotEquals(0, memoryUsed);
|
||||
}
|
||||
}
|
@ -1,82 +0,0 @@
|
||||
/*
|
||||
* 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 cn.hippo4j.config.toolkit;
|
||||
|
||||
import cn.hippo4j.common.toolkit.CollectionUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
/**
|
||||
* Map util.
|
||||
*/
|
||||
public class MapUtil {
|
||||
|
||||
public static Object computeIfAbsent(Map target, Object key, BiFunction mappingFunction, Object param1, Object param2) {
|
||||
Objects.requireNonNull(target, "target");
|
||||
Objects.requireNonNull(key, "key");
|
||||
Objects.requireNonNull(mappingFunction, "mappingFunction");
|
||||
Objects.requireNonNull(param1, "param1");
|
||||
Objects.requireNonNull(param2, "param2");
|
||||
Object val = target.get(key);
|
||||
if (val == null) {
|
||||
Object ret = mappingFunction.apply(param1, param2);
|
||||
target.put(key, ret);
|
||||
return ret;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fuzzy matching based on Key.
|
||||
*
|
||||
* @param sourceMap
|
||||
* @param filters
|
||||
* @return
|
||||
*/
|
||||
public static List<String> parseMapForFilter(Map<String, ?> sourceMap, String filters) {
|
||||
List<String> resultList = new ArrayList<>();
|
||||
if (CollectionUtil.isEmpty(sourceMap)) {
|
||||
return resultList;
|
||||
}
|
||||
sourceMap.forEach((key, val) -> {
|
||||
if (checkKey(key, filters)) {
|
||||
resultList.add(key);
|
||||
}
|
||||
});
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Match the characters you want to query.
|
||||
*
|
||||
* @param key
|
||||
* @param filters
|
||||
* @return
|
||||
*/
|
||||
private static boolean checkKey(String key, String filters) {
|
||||
if (key.indexOf(filters) > -1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue