Skip to content

A Java common utility library that simplifies the operation of other libraries by encapsulating its original functionality.

Notifications You must be signed in to change notification settings

taogen-lib/taogen-commons

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Taogen Commons Java

A Java common utility library that simplifies the operation of other libraries by encapsulating its original functionality.

Dependency this library in Maven Project

  • Add the JitPack repository to your build file

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>
  • Add the dependency

    Using release tag

    <dependency>
        <groupId>com.github.taogen-lib.taogen-commons</groupId>
        <artifactId>commons-lang</artifactId>
        <version>Tag</version>
    </dependency>

    Or using a short commit hash on JitPack

    <dependency>
        <groupId>com.github.taogen-lib.taogen-commons</groupId>
        <artifactId>commons-lang</artifactId>
        <version>{short_commit_hash_on_jitpack}</version>
    </dependency>

    Or using the latest version (After you push a new commit. Other projects need to force checking update mvn clean install -U and reload maven project)

    <dependency>
      <groupId>com.github.taogen-lib.taogen-commons</groupId>
      <artifactId>commons-lang</artifactId>
      <version>-SNAPSHOT</version>
    </dependency>

Or using the local Maven repository

<dependency>
  <groupId>com.taogen</groupId>
  <artifactId>commons-lang</artifactId>
  <version>{some_version}</version>
</dependency>

Test

# test all modules
mvn clean test
# test a specific module
mvn clean test -pl submodule

Install

# Install all modules to local repository. So other projects can refer to it and grab it from your local repository.
mvn clean install
# Install a specific module
mvn clean install -pl submodule

Method return null or throw an exception

If you are always expecting to find a value then throw the exception if it is missing. The exception would mean that there was a problem.

If the value can be missing or present and both are valid for the application logic then return a null.

More important: What do you do other places in the code? Consistency is important.

Content

Main

A Utility Accomplishment Process

Defining methods of utility --> writing fake implement class --> writing test class --> writing real implement class and pass its test.

Test Case Conventions

  • Illegal parameters
  • bound value + middle value
  • Cover all DD-paths

Assertions of Junit 5

assertAll(...)
assertEquals(...)
assertNotEquals(...)
assertArrayEquals(...)
assertTrue(...)
assertFalse(...)
assertNull(...)
assertNotNull(...)
assertSame(...) 
assertNotSame(...)
assertIterableEquals(...)
assertLinesMatch(...)
assertThrows(...)
assertTimeout(...)
assertTimeoutPreemptively(...)
fail(...)

StringUtils

Dependencies:

import java.lang.reflect.InvocationTargetException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Methods

Convert

int str2Int (String str, int default)
long str2Long(String str, long default)
double str2Double(String str, double default)
boolean Str2Boolean(String str, boolean default)

Verify

boolean isEmpty(String str)
boolean isEmail(String email)   
boolean isMobile(String mobile)
boolean isMatch(String str, String regex)

Handle

String encodeMobile(String mobile)  
String encodeEmail(String email)
String encodeBankCard(String bankcard)
String jointString (String... strs)
String toNotNull (String str)
String expandLength(String str, int len, char fillChar)

back to content


EncodingUtils

Dependencies

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Methods

Verify

boolean isChinese(String str)
boolean isEmoji(String str)

Convert

String iso2Utf8(String str)
String emojiConvert(String str)  
String emojiRecovery(String str)

back to content


NumberUtils

Dependencies

Methods

back to content


RandomUtils

Dependencies

import java.util.Random;

Methods

int getRandomNumber(int bound)
String getRandomNumberStr(int length)
String getMixedRandomStr(int length)

back to content


DateUtils

Dependencies

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

Methods

Convert

//int <--> String <--> Date
String getDateFormatStr(Date date, DateFormat format)
Date getDateByStr (String dateStr, DateFormat format)  
String convertDateStr(String dateStr, DateFormat fromFormat, DateFormat toFormat)

Calculate

int getField(Date date, int calendarField)
Date add(Date date, int calendarField, int addtion)
long getDiffDays(Date firstDate, Date secondDate)
List<String> getBetweenDates(Date startDate, Date endDate, DateFormat formatter)
List<String> getBetweenMonths(Date startDate, Date endDate, DateFormat formatter)
Date getFirstDayOfWeek(Date date)
Date getFirstDayOfMonth(Date date) 
Date getLastDayOfMonth(Date date)
Date getLastDayOfWeek(Date date) 
int getWeekOfMonth(Date date)  
int getWeekOfYear(Date date)

back to content


TimeUtils

Dependencies

Methods

Timestamp getCurrentTime()
int getHourDiff (long startTime, long endTime)
String getTimeZone()

back to content


EncryptUtils

Dependencies

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

Methods

String encryptBASE64 (String source)
String decryptBASE64 (String str)
String generateSalt()

back to content


Md5Utils

Dependencies

Methods

back to content


PropertyUtils

Dependencies

import java.io.InputStream;
import java.util.Properties;

Methods

void load(String filePath)
String getString (String key)
int getInt (String key, int def)
long getLong (String key, long def)

back to content


ImageUtils

Dependencies

import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
java.io.*;

Methods

boolean isImageFile(String fileExt)
byte[] resize(byte[] img, int width, int height)

back to content


LocationUtils

back to content


FileUtils

Dependencies

Methods

back to content


ZipUtils

Dependencies

Methods

back to content


I18nUtils

Dependencies

Methods

back to content


javax

IpUtils

Dependencies

import javax.servlet.http.HttpServletRequest;

Methods

String getIpAddr (HttpServletRequest request)
String getRealIpAddr (HttpServletRequest request)

back to content


RequestParamUtils

back to content


EmailUtils

back to content


Third Library

JsonUtils

Dependencies

import java.lang.reflect.Type;
import com.google.gson.Gson;

Methods

String toJson (Object obj)
T fromJson (String str, Type type)
T fromJson (String str, Class<T> type)

back to content


XmlUtils

Dependencies

Methods

back to content


LogUtils

Dependencies

import org.apache.log4j.Logger;

Methods

void debug (String message)
void info (String message)
void warn (String message)
void error (String message)

back to content


PinyinUtils

Dependencies

import net.sourceforge.pinyin4j.PinyinHelper;

Methods

String getPinyin (String str)
String getPinyinHeadChar (String str)

back to content


HttpUtils

Dependencies

Methods

back to content


QrCodeUtils

Dependencies

Methods

back to content


SpringUtils

Dependencies

Methods

back to content


--END--

About

A Java common utility library that simplifies the operation of other libraries by encapsulating its original functionality.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages