JS 与 trick 代码的运用及解析全攻略
552
2022-10-23
一个Android库提供了常用工具和实用程序
Toolbelt is an Android library for common tools and utilities for optional values, easier logging and operations on Strings. I need these mostly boilerplate stuff for almost all of my Android projects so I decided to serve them as a library.
How to Include In Your Project?
Toolbelt is served on Bintray and jCenter. So, simply add following
compile 'com.github.mehmetakiftutuncu:toolbelt:latestVersion'
inside dependencies in build.gradle file in your application module. If somehow dependency resolution fails, you should also add the following repository
maven {url 'https://dl.bintray.com/mehmetakiftutuncu/maven'}
inside repositories. Don't forget to replace latestVersion with the latest version number found in the title.
What's In It?
1. Optional
It represents an optional value that may or may not exist. This is an attempt to avoid using nulls as they are not safe.
// Creates an existing optionalOptional> optional3 = Optional.with(null);optional1.get(); // Yields "Hello"optional2.get(); // Throws a NoSuchElementException because value does not existoptional1.getOrElse("World"); // Yields "Hello"optional3.getOrElse("World"); // Yields "World"optional1.isDefined(); // Yields trueoptional2.isDefined(); // Yields falseoptional1.isEmpty(); // Yields falseoptional2.isEmpty(); // Yields trueOptional
2. Log
It wraps android.util.Log methods to get dynamic tags using the caller reference and supports message formatting as done in String.format(). It also simplifies log levels to just DEBUG, WARN and ERROR.
// Debug level log methodspublic static void debug(String tag, String message, Object... args)public static void debug(Class> caller, String message, Object... args)// Warn level log methodspublic static void warn(String tag, String message, Object... args)public static void warn(Class> caller, String message, Object... args)// Error level log methods with and without a Throwablepublic static void error(String tag, String message, Object... args)public static void error(String tag, Throwable throwable, String message, Object... args)public static void error(Class> caller, String message, Object... args)public static void error(Class> caller, Throwable throwable, String message, Object... args)/* Example call with dynamic tag from a non-static scope where 'this' is accessible, for example in an instance method of class Foo * * This would log following with debug level: * * Tag | Message * ----|-------- * Foo | Doing some work with 'test' and '123'... */Log.debug(this, "Doing some work with '%s' and '%d'...", "test", 123);/* Example call with dynamic tag from a static scope where 'this' is inaccessible, for example in a static method of class Bar * * This would log following with warn level: * * Tag | Message * ----|-------- * Bar | Doing some work with 'test' and '123'... */Log.warn(Bar.class, "Doing some work with '%s' and '%d'...", "test", 123);
3. StringUtilities
Name says it all.
StringUtilities.isEmpty(null); // Yields trueStringUtilities.isEmpty(""); // Yields trueStringUtilities.isEmpty("foo"); // Yields falseList
4. Timer
You can use it to time stuff and see how long they take to run.
// Manage start and stop yourselfTimer.start("foo");// Do some worklong duration = Timer.stop("foo");printf("Finished in %d milliseconds!", duration);// Or wrap a block of work and time it automaticallyTimer.ActionResult
Contribution
Please feel free to and contribute. I'd love to hear your feedback, issue reports and pull requests.
License
Toolbelt is licensed under Apache License Version 2.0.
Copyright (C) 2016 Mehmet Akif Tütüncü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 athttp://apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License.
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~