提交 fe70516b 编写于 作者: B Blankj

see 01/03 log

上级 df832451
......@@ -13,8 +13,9 @@ import com.google.gson.GsonBuilder
*/
final class JsonUtils {
static final Gson GSON = new GsonBuilder().setPrettyPrinting().create()
static String getFormatJson(Object object) {
Gson gson = new GsonBuilder().setPrettyPrinting().create()
return gson.toJson(object)
return GSON.toJson(object)
}
}
......@@ -34,7 +34,6 @@ import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
......@@ -52,7 +51,7 @@ public final class CacheDiskUtils implements CacheConstants {
private static final int DEFAULT_MAX_COUNT = Integer.MAX_VALUE;
private static final String CACHE_PREFIX = "cdu";
private static final Map<String, CacheDiskUtils> CACHE_MAP = new ConcurrentHashMap<>();
private static final Map<String, CacheDiskUtils> CACHE_MAP = new HashMap<>();
private final String mCacheKey;
private final File mCacheDir;
......@@ -138,8 +137,13 @@ public final class CacheDiskUtils implements CacheConstants {
final String cacheKey = cacheDir.getAbsoluteFile() + "_" + maxSize + "_" + maxCount;
CacheDiskUtils cache = CACHE_MAP.get(cacheKey);
if (cache == null) {
cache = new CacheDiskUtils(cacheKey, cacheDir, maxSize, maxCount);
CACHE_MAP.put(cacheKey, cache);
synchronized (CacheDiskUtils.class) {
cache = CACHE_MAP.get(cacheKey);
if (cache == null) {
cache = new CacheDiskUtils(cacheKey, cacheDir, maxSize, maxCount);
CACHE_MAP.put(cacheKey, cache);
}
}
}
return cache;
}
......
......@@ -11,8 +11,8 @@ import org.json.JSONArray;
import org.json.JSONObject;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* <pre>
......@@ -24,7 +24,7 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public final class CacheDoubleUtils implements CacheConstants {
private static final Map<String, CacheDoubleUtils> CACHE_MAP = new ConcurrentHashMap<>();
private static final Map<String, CacheDoubleUtils> CACHE_MAP = new HashMap<>();
private final CacheMemoryUtils mCacheMemoryUtils;
private final CacheDiskUtils mCacheDiskUtils;
......@@ -50,8 +50,13 @@ public final class CacheDoubleUtils implements CacheConstants {
final String cacheKey = cacheDiskUtils.toString() + "_" + cacheMemoryUtils.toString();
CacheDoubleUtils cache = CACHE_MAP.get(cacheKey);
if (cache == null) {
cache = new CacheDoubleUtils(cacheMemoryUtils, cacheDiskUtils);
CACHE_MAP.put(cacheKey, cache);
synchronized (CacheDoubleUtils.class) {
cache = CACHE_MAP.get(cacheKey);
if (cache == null) {
cache = new CacheDoubleUtils(cacheMemoryUtils, cacheDiskUtils);
CACHE_MAP.put(cacheKey, cache);
}
}
}
return cache;
}
......
......@@ -5,8 +5,8 @@ import android.support.v4.util.LruCache;
import com.blankj.utilcode.constant.CacheConstants;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* <pre>
......@@ -20,7 +20,7 @@ public final class CacheMemoryUtils implements CacheConstants {
private static final int DEFAULT_MAX_COUNT = 256;
private static final Map<String, CacheMemoryUtils> CACHE_MAP = new ConcurrentHashMap<>();
private static final Map<String, CacheMemoryUtils> CACHE_MAP = new HashMap<>();
private final String mCacheKey;
private final LruCache<String, CacheValue> mMemoryCache;
......@@ -54,8 +54,13 @@ public final class CacheMemoryUtils implements CacheConstants {
public static CacheMemoryUtils getInstance(final String cacheKey, final int maxCount) {
CacheMemoryUtils cache = CACHE_MAP.get(cacheKey);
if (cache == null) {
cache = new CacheMemoryUtils(cacheKey, new LruCache<String, CacheValue>(maxCount));
CACHE_MAP.put(cacheKey, cache);
synchronized (CacheMemoryUtils.class) {
cache = CACHE_MAP.get(cacheKey);
if (cache == null) {
cache = new CacheMemoryUtils(cacheKey, new LruCache<String, CacheValue>(maxCount));
CACHE_MAP.put(cacheKey, cache);
}
}
}
return cache;
}
......
......@@ -33,7 +33,6 @@ import java.io.StringReader;
import java.io.StringWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Array;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.UnknownHostException;
......@@ -103,7 +102,8 @@ public final class LogUtils {
private static final String ARGS = "args";
private static final String PLACEHOLDER = " ";
private static final Config CONFIG = new Config();
private static final Gson GSON = new GsonBuilder().serializeNulls().create();
private static final Gson GSON = new GsonBuilder()
.setPrettyPrinting().serializeNulls().create();
private static final ThreadLocal<SimpleDateFormat> SDF_THREAD_LOCAL = new ThreadLocal<>();
......@@ -794,7 +794,10 @@ public final class LogUtils {
}
static String object2Json(Object object) {
return formatJson(GSON.toJson(object));
if (object instanceof CharSequence) {
return formatJson(object.toString());
}
return GSON.toJson(object);
}
static String formatXml(String xml) {
......@@ -803,7 +806,7 @@ public final class LogUtils {
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
xml = xmlOutput.getWriter().toString().replaceFirst(">", ">" + LINE_SEP);
} catch (Exception e) {
......@@ -966,9 +969,9 @@ public final class LogUtils {
private static String formatJson(String json) {
try {
if (json.startsWith("{")) {
json = new JSONObject(json).toString(4);
json = new JSONObject(json).toString(2);
} else if (json.startsWith("[")) {
json = new JSONArray(json).toString(4);
json = new JSONArray(json).toString(2);
}
} catch (JSONException e) {
e.printStackTrace();
......
......@@ -6,9 +6,9 @@ import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* <pre>
......@@ -21,7 +21,7 @@ import java.util.concurrent.ConcurrentHashMap;
@SuppressLint("ApplySharedPref")
public final class SPUtils {
private static final Map<String, SPUtils> SP_UTILS_MAP = new ConcurrentHashMap<>();
private static final Map<String, SPUtils> SP_UTILS_MAP = new HashMap<>();
private SharedPreferences sp;
/**
......@@ -64,8 +64,13 @@ public final class SPUtils {
if (isSpace(spName)) spName = "spUtils";
SPUtils spUtils = SP_UTILS_MAP.get(spName);
if (spUtils == null) {
spUtils = new SPUtils(spName, mode);
SP_UTILS_MAP.put(spName, spUtils);
synchronized (SPUtils.class) {
spUtils = SP_UTILS_MAP.get(spName);
if (spUtils == null) {
spUtils = new SPUtils(spName, mode);
SP_UTILS_MAP.put(spName, spUtils);
}
}
}
return spUtils;
}
......
......@@ -6,9 +6,10 @@ import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.util.SparseArray;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
......@@ -29,10 +30,8 @@ import java.util.concurrent.atomic.AtomicLong;
*/
public final class ThreadUtils {
private static final Map<Integer, Map<Integer, ExecutorService>> TYPE_PRIORITY_POOLS =
new ConcurrentHashMap<>();
private static final Map<Task, ScheduledExecutorService> TASK_SCHEDULED =
new ConcurrentHashMap<>();
private static final SparseArray<SparseArray<ExecutorService>> TYPE_PRIORITY_POOLS = new SparseArray<>();
private static final Map<Task, ScheduledExecutorService> TASK_SCHEDULED = new HashMap<>();
private static final byte TYPE_SINGLE = -1;
private static final byte TYPE_CACHED = -2;
......@@ -878,7 +877,7 @@ public final class ThreadUtils {
}, initialDelay, period, unit);
}
private static ScheduledExecutorService getScheduledByTask(final Task task) {
private synchronized static ScheduledExecutorService getScheduledByTask(final Task task) {
ScheduledExecutorService scheduled = TASK_SCHEDULED.get(task);
if (scheduled == null) {
UtilsThreadFactory factory = new UtilsThreadFactory("scheduled", Thread.MAX_PRIORITY);
......@@ -888,7 +887,7 @@ public final class ThreadUtils {
return scheduled;
}
private static void removeScheduleByTask(final Task task) {
private synchronized static void removeScheduleByTask(final Task task) {
ScheduledExecutorService scheduled = TASK_SCHEDULED.get(task);
if (scheduled != null) {
TASK_SCHEDULED.remove(task);
......@@ -900,11 +899,11 @@ public final class ThreadUtils {
return getPoolByTypeAndPriority(type, Thread.NORM_PRIORITY);
}
private static ExecutorService getPoolByTypeAndPriority(final int type, final int priority) {
private synchronized static ExecutorService getPoolByTypeAndPriority(final int type, final int priority) {
ExecutorService pool;
Map<Integer, ExecutorService> priorityPools = TYPE_PRIORITY_POOLS.get(type);
SparseArray<ExecutorService> priorityPools = TYPE_PRIORITY_POOLS.get(type);
if (priorityPools == null) {
priorityPools = new ConcurrentHashMap<>();
priorityPools = new SparseArray<>();
pool = createPoolByTypeAndPriority(type, priority);
priorityPools.put(priority, pool);
TYPE_PRIORITY_POOLS.put(type, priorityPools);
......@@ -921,6 +920,7 @@ public final class ThreadUtils {
private static ExecutorService createPoolByTypeAndPriority(final int type, final int priority) {
switch (type) {
case TYPE_SINGLE:
System.out.println("hehe");
return Executors.newSingleThreadExecutor(
new UtilsThreadFactory("single", priority)
);
......
......@@ -11,13 +11,13 @@ import android.support.v4.content.FileProvider;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* <pre>
......@@ -158,10 +158,9 @@ public final class Utils {
static class ActivityLifecycleImpl implements ActivityLifecycleCallbacks {
final LinkedList<Activity> mActivityList = new LinkedList<>();
final Map<Object, OnAppStatusChangedListener> mStatusListenerMap = new ConcurrentHashMap<>();
final Map<Activity, Set<OnActivityDestroyedListener>> mDestroyedListenerMap = new ConcurrentHashMap<>();
final LinkedList<Activity> mActivityList = new LinkedList<>();
final Map<Object, OnAppStatusChangedListener> mStatusListenerMap = new HashMap<>();
final Map<Activity, Set<OnActivityDestroyedListener>> mDestroyedListenerMap = new HashMap<>();
private int mForegroundCount = 0;
private int mConfigCount = 0;
......
package com.blankj.utilcode.util;
import android.util.Log;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
......@@ -29,9 +27,6 @@ public class BaseTest {
@Test
public void test() throws Exception {
Log.e("haha", "test: ");
// final CountDownLatch countDownLatch = new CountDownLatch(1);
// final Scanner scanner = new Scanner(System.in);
// ExecutorService singlePool = ThreadUtils.getSinglePool();
......
package com.blankj.utilcode.util;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* <pre>
* author: Blankj
......@@ -12,10 +20,16 @@ import org.junit.Test;
*/
public class LogUtilsTest extends BaseTest {
private static final String JSON = "{\"tools\": [{ \"name\":\"css format\" , \"site\":\"http://tools.w3cschool.cn/code/css\" },{ \"name\":\"JSON format\" , \"site\":\"http://tools.w3cschool.cn/code/JSON\" },{ \"name\":\"pwd check\" , \"site\":\"http://tools.w3cschool.cn/password/my_password_safe\" }]}";
private static final String JSON = "{\"tools\": [{ \"name\":\"css format\" , \"site\":\"http://tools.w3cschool.cn/code/css\" },{ \"name\":\"JSON format\" , \"site\":\"http://tools.w3cschool.cn/code/JSON\" },{ \"name\":\"pwd check\" , \"site\":\"http://tools.w3cschool.cn/password/my_password_safe\" }]}";
private static final String XML = "<books><book><author>Jack Herrington</author><title>PHP Hacks</title><publisher>O'Reilly</publisher></book><book><author>Jack Herrington</author><title>Podcasting Hacks</title><publisher>O'Reilly</publisher></book></books>";
private static final int[] ONE_D_ARRAY = new int[]{1, 2, 3};
private static final int[][] TWO_D_ARRAY = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
private static final ArrayList<String> LIST = new ArrayList<>();
private static final Map<String, String> MAP = new HashMap<>();
@Test
public void testV() {
LogUtils.v();
LogUtils.v("");
LogUtils.v(null);
LogUtils.v("hello");
......@@ -25,6 +39,7 @@ public class LogUtilsTest extends BaseTest {
@Test
public void testVTag() {
LogUtils.vTag("");
LogUtils.vTag("", "");
LogUtils.vTag("TAG", null);
LogUtils.vTag("TAG", "hello");
......@@ -34,6 +49,7 @@ public class LogUtilsTest extends BaseTest {
@Test
public void testD() {
LogUtils.d();
LogUtils.d("");
LogUtils.d(null);
LogUtils.d("hello");
......@@ -43,6 +59,7 @@ public class LogUtilsTest extends BaseTest {
@Test
public void testDTag() {
LogUtils.dTag("");
LogUtils.dTag("", "");
LogUtils.dTag("TAG", null);
LogUtils.dTag("TAG", "hello");
......@@ -52,6 +69,7 @@ public class LogUtilsTest extends BaseTest {
@Test
public void testI() {
LogUtils.i();
LogUtils.i("");
LogUtils.i(null);
LogUtils.i("hello");
......@@ -61,6 +79,7 @@ public class LogUtilsTest extends BaseTest {
@Test
public void testITag() {
LogUtils.iTag("");
LogUtils.iTag("", "");
LogUtils.iTag("TAG", null);
LogUtils.iTag("TAG", "hello");
......@@ -70,6 +89,7 @@ public class LogUtilsTest extends BaseTest {
@Test
public void testW() {
LogUtils.w();
LogUtils.w("");
LogUtils.w(null);
LogUtils.w("hello");
......@@ -79,6 +99,7 @@ public class LogUtilsTest extends BaseTest {
@Test
public void testWTag() {
LogUtils.wTag("");
LogUtils.wTag("", "");
LogUtils.wTag("TAG", null);
LogUtils.wTag("TAG", "hello");
......@@ -88,6 +109,7 @@ public class LogUtilsTest extends BaseTest {
@Test
public void testE() {
LogUtils.e();
LogUtils.e("");
LogUtils.e(null);
LogUtils.e("hello");
......@@ -97,6 +119,7 @@ public class LogUtilsTest extends BaseTest {
@Test
public void testETag() {
LogUtils.eTag("");
LogUtils.eTag("", "");
LogUtils.eTag("TAG", null);
LogUtils.eTag("TAG", "hello");
......@@ -106,6 +129,7 @@ public class LogUtilsTest extends BaseTest {
@Test
public void testA() {
LogUtils.a();
LogUtils.a("");
LogUtils.a(null);
LogUtils.a("hello");
......@@ -115,6 +139,7 @@ public class LogUtilsTest extends BaseTest {
@Test
public void testATag() {
LogUtils.aTag("");
LogUtils.aTag("", "");
LogUtils.aTag("TAG", null);
LogUtils.aTag("TAG", "hello");
......@@ -124,14 +149,68 @@ public class LogUtilsTest extends BaseTest {
@Test
public void testJson() {
// LogUtils.json(JSON);
LogUtils.json(new Person("B\nlankj"));
LogUtils.json(JSON);
LogUtils.json(new Person("Blankj"));
}
@Test
public void testXml() {
LogUtils.xml(XML);
}
@Test
public void testObject() {
LIST.add("hello");
LIST.add("log");
LIST.add("utils");
MAP.put("name", "AndroidUtilCode");
MAP.put("class", "LogUtils");
LogUtils.d((Object) ONE_D_ARRAY);
LogUtils.d((Object) TWO_D_ARRAY);
LogUtils.d(LIST);
LogUtils.d(MAP);
LogUtils.d(formatJson(array2String(TWO_D_ARRAY)));
}
private static String formatJson(String json) {
try {
if (json.startsWith("{")) {
json = new JSONObject(json).toString(2);
} else if (json.startsWith("[")) {
json = new JSONArray(json).toString(2);
}
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
static String array2String(Object object) {
if (object instanceof Object[]) {
return Arrays.deepToString((Object[]) object);
} else if (object instanceof boolean[]) {
return Arrays.toString((boolean[]) object);
} else if (object instanceof byte[]) {
return Arrays.toString((byte[]) object);
} else if (object instanceof char[]) {
return Arrays.toString((char[]) object);
} else if (object instanceof double[]) {
return Arrays.toString((double[]) object);
} else if (object instanceof float[]) {
return Arrays.toString((float[]) object);
} else if (object instanceof int[]) {
return Arrays.toString((int[]) object);
} else if (object instanceof long[]) {
return Arrays.toString((long[]) object);
} else if (object instanceof short[]) {
return Arrays.toString((short[]) object);
}
throw new IllegalArgumentException("Array has incompatible type: " + object.getClass());
}
static class Person {
String name;
......
......@@ -53,7 +53,6 @@ public class LogActivity extends BaseBackActivity {
private static final Intent INTENT = new Intent();
private static final ArrayList<String> LIST = new ArrayList<>();
private static final Map<String, String> MAP = new HashMap<>();
private static final Object OBJECT = new Config();
private static final String LONG_STR;
......@@ -298,9 +297,6 @@ public class LogActivity extends BaseBackActivity {
} else if (i1 == R.id.btn_log_map) {
LogUtils.e(MAP);
} else if (i1 == R.id.btn_log_object) {
LogUtils.e(OBJECT);
}
}
......
......@@ -188,11 +188,4 @@
android:layout_height="wrap_content"
android:text="@string/log_map" />
<Button
android:id="@+id/btn_log_object"
style="@style/WideBtnStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/log_object" />
</LinearLayout>
\ No newline at end of file
......@@ -208,7 +208,6 @@
<string name="log_intent">Log Intent</string>
<string name="log_array_list">Log Array List</string>
<string name="log_map">Log Map</string>
<string name="log_object">Log Object</string>
<!--Permission 相关-->
<string name="permission_open_app_settings">Open App Settings</string>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册