GithubHelp home page GithubHelp logo

slotstring's Introduction

SlotString

FSM-based template string formatting classes implemented using only native Java features, with little to no counter-intuitive implicit conversion features, are used in place of MessageFormat.format().

Current source code version: 0.2.3.

Usage

Utility source see SlotString.java.

Example see SlotStringTest.javaใ€‚

If it is not appropriate to add the whole class to your project, you can copy the source code of the qformat method to your class and manually inline the source code of the parse and parseQformat methods, and manually inline and adjust the multithreading related fields and judgment code according to the actual multithreading usage. If you are using it in a multi-threaded scenario, you can also move the two buffers to the class as fields and change the type to StringBuffer.

Example

public void queryVoucher() {
  SlotString slot = new SlotString();
  Map<String, Object> params = new HashMap<>();
  params.put("YEAR", 2023);
  // sql1 = "SELECT * FROM VOUCHER2023 WHERE COMMENT = ''"
  String sql1 = slot.qformat("SELECT * FROM VOUCHER{YEAR} WHERE COMMENT = ''", params);

  queryVoucherDetail(slot, params);
}

private void queryVoucherDetail(SlotString slot, Map<String, Object> params) {
  params.put("YEAR", 2020);
  // sql2 = "SELECT * FROM VOUCHER_DETAIL2020"
  String sql2 = slot.qformat("SELECT * FROM VOUCHER_DETAIL{YEAR}", params);
}
private static final SlotString QUERY_VOUCHER_SQL = new SlotString("SELECT * FROM VOUCHER{YEAR}");

public void queryVoucher() {
  Map<String, Object> params = new HashMap<>();
  params.put("YEAR", 2023);
  // sql1 = "SELECT * FROM VOUCHER2023"
  String sql1 = slot.format(params);

  params.put("YEAR", 2020);
  // sql2 = "SELECT * FROM VOUCHER2020"
  String sql2 = slot.format(params);
}
// enable multi-thread support
private static final SlotString SLOT = new SlotString(true);

@PostMapping("/api1")
@RequestBody
public String api1() {
  Map<String, Object> params = new HashMap<>();
  params.put("YEAR", 2023);
  // "SELECT * FROM VOUCHER2023"
  return SLOT.qformat("SELECT * FROM VOUCHER{YEAR}", params);
}

@PostMapping("/api2")
@RequestBody
public String api2() {
  Map<String, Object> params = new HashMap<>();
  params.put("YEAR", 2020);
  // "SELECT * FROM VOUCHER_DETAIL2020"
  return SLOT.qformat("SELECT * FROM VOUCHER_DETAIL{YEAR}", params);
}

Performance

500000 loops are tested on personal computer, for reference only.

See SlotStringTest.testPerformance()

[I][testPerformance] Starting Performance Test (Loop Times: 500000)
[I][testPerformance:samePattern] Elapsed time of shared buffer qformat (ms): 5603
[I][testPerformance:samePattern] Elapsed time of discrete buffer qformat (ms): 6318
[I][testPerformance:samePattern] Elapsed time of compile+format (ms): 1838
[I][testPerformance:diffPattern] Elapsed time of shared buffer qformat (ms): 6872
[I][testPerformance:diffPattern] Elapsed time of discrete buffer qformat (ms): 7663
[I][testPerformance:diffPattern] Elapsed time of compile+format (ms): 8496
[I][testPerformance:ratio] Elapsed time ratio for Shared-Buffer qformat, Discrete-Buffer qformat and compile+format:
  Same Pattern: 0.8868312757201646, 1.0, 0.290914846470402
  Diff Pattern: 0.8088512241054614, 0.9019538606403014, 1.0

License

The MIT License.

slotstring's People

Contributors

rihalvnium76 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.