Skip to content

seepine/tool

Repository files navigation

tool

一、安装

LatestVersion

Maven Central

Maven

<dependency>
  <groupId>com.seepine</groupId>
  <artifactId>tool</artifactId>
  <version>${latestVersion}</version>
</dependency>

Gradle

implementation("com.seepine:tool:${lastVersion}")

二、用法

run

class RunTest {
  public static void main(String[] args) {
    String str = " hello world ";
    Run.isBlank(str, () -> System.out.println("为空则进入方法体"));
    Run.nonBlank(
      str,
      (val -> {
        System.out.println("不为空则进入方法体");
        System.out.println(val);
      }));
    Run.nonBlankAndTrim(
      str,
      (val -> {
        System.out.println("不为空则进入方法体,并执行trim");
        System.out.println(val);
      }));
  }
}

validate

class Test {
  public static void main(String[] args) {
    Validate.isNull(null, "对象必须为null");
    Validate.nonBlank(" ", "对象必须不为blank");
  }
}

secure

class Test {
  public static void main(String[] args) {
    RSA rsa = new RSA();
    // rsa.privateEncrypt(src)

    AES aes = new AES("key");
    // aes.encrypt(src);

    Base64.encode("data");
    Base64.decode("data");
  }
}

builder

class BuilderTest {
  public static void main(String[] args) {
    User user =
      Builder.of(User::new).with(User::setName, "zhangSan").with(User::setAge, 27).build();
    System.out.println(user.toString());
    // User{name='zhangSan', age=27}

    User user2 = Builder.of(User::new).with(User::setAll, "liSi", 26).build();
    System.out.println(user2.toString());
    // User{name='liSi', age=26}
  }
}

Lock

class LockTest {
  public static void main(String[] args) {
    Lock.sync(1, () -> System.out.println("run end 1"));
    String str =
      Lock.sync(
        1,
        () -> {
          String a = "a";
          return a + "b";
        });

    // 还可自定义锁的实现,例如增强为分布式锁等
    Lock.enhance(new LockService() {
      // ...
    });
  }
}

Cache

public class CacheTest {
  public static void main(String[] args) {
    Cache.set("test", "test value", 1500);
    try {
      TimeUnit.MILLISECONDS.sleep(1000);
    } catch (InterruptedException ignore) {
    }
    System.out.println(Cache.getStr("test"));
    // test value,因为还没过期

    try {
      TimeUnit.MILLISECONDS.sleep(600);
    } catch (InterruptedException ignore) {
    }
    System.out.println(Cache.getStr("test"));
    // null,因为过期了

    System.out.println(Cache.get("test", () -> "new test value", 0));
    // new test value,因为过期了,所以取默认填充的值

    System.out.println(Cache.get("test", () -> "more new value", 0));
    // new test value,因为上次默认值保存到缓存了没过期

    // 可实现CacheService重写缓存逻辑例如缓存到redis等,并在程序初始化时调用Cache.enhance进行增强
    // 而后的Cache.get/Cache.set都会走自定义的CacheService
    // Cache.enhance(new CacheService() {});
  }
}

SSE

对称可搜索加密算法(Symmetric Searchable Encryption),原理通过对字符分割aes加密,实现密文保存,又可模糊搜索。

class SSETest{
  public static void main(String[]args){
    String secretKey = "abcdefghijklmnop";
    String text = "这是明文内容,今天天气真不错...";
    String encryptData = SSE.create(secretKey).encode(text);
    // 加密结果类似 i2dQODC/na9eW3uG8rakxw==#i2dQODC/na9eW3uG8rakxw==#...
    // 数据库保存的是密文 INSERT INTO table_name (text, ...) VALUES (encryptData, ...);
    // 展示时解密
    String originText = SSE.create(secretKey).decode(encryptData);

    // 搜索时对关键词加密
    String searchKey = SSE.create(secretKey).encode("天气");
    // SELECT * FROM table_name WHERE text LIKE '%searchKey%'
  }
}

About

tool package

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages