Git Product home page Git Product logo

primitives's Introduction

Travis Codecov Codacy grade Github All Releases

Quick Start --- Usage --- License

  • Dreaming of a simple way to convert byte[] to int[]?

    How about that:

    int[] example = IntArrays.from(new byte[] { 1, 2, 3 });
  • You need to concatenate two char arrays?

    Nothing easier than that!

    char[] pt1 = new char[] { 'h', 'e', 'l' };
    char[] pt2 = new char[] { 'l', 'o' };
    char[] result = CharArrays.concatAll(pt1, pt2);
  • String.join() doesn't work for your int array?

    It might not, but the following code sure does!

    int[] input = new int[] { 3, 2, 1 };
    String result = IntArrays.join(";", input);

Primitives is everything you'll ever need to work with primitive types!

Quick Start

Installation

  1. Download the binary jar file from the latest release on GitHub
  • Additionally download the source and/or javadoc jar if needed
  • Add the binary jar to your projects classpath

Usage

Available Classes

Package ch.deletescape.primitives

  • Bools
  • Bytes
  • Chars
  • Doubles
  • Floats
  • Ints
  • Longs
  • Shorts
  • Strings

Package ch.deletescape.primitives.arrays

  • BoolArrays
  • ByteArrays
  • CharArrays
  • DoubleArrays
  • FloatArrays
  • IntArrays
  • LongArrays
  • ShortArrays

Conversions

In the following example an int value is converted to short using Shorts, all other conversions work exactly the same way.

int i = 1;
short s = Shorts.from(i);

boolean Conversions

Conversions from boolean return a value of 1 for true and a value of 0 for false. When converting to boolean a value of 1 returns true, any other value will result in false. The same applies to boolean[] conversions.

Random

The next small code snippet shows how to generate a pseudorandom int value using Ints, random generation for other primitive types work the same way.

int i = Ints.random();

Random Arrays

The difference when generating an array of random values, for example a byte array, is that the size of the array needs to be specified.

byte[] ba = ByteArrays.random(5);

Array Concatenation

To concate two or more boolean arrays using BoolArrays you can refer to the following piece of code. Array concatenations of other types follow the same pattern.

boolean[] ba1 = new boolean[] { true, false };
boolean[] ba2 = new boolean[] { false, false };
boolean[] merge = BoolArrays.concatAll(ba1, ba2);

Array Joining

The following code shows how to easily convert an int array to a beautiful String, the same way String.join() works for CharSequence items. This can also be used for arrays of all other types.

int[] ia = new int[] { 1, 4, 5 };
String str = IntArrays.join("; ", ia);

Array Contains

You can check if a short array contains a certain value like this:

short[] sa = new short[] { 1, 2, 3 };
boolean contains = ShortArrays.contains(sa, Shorts.from(2));

The same pattern can be applied to all the other types.

Array Distinct

To get only the unique values in a long array, you can use the following code which also applies to all other types.

long[] la = new long[] { 5, 3, 4, 5, 4, 5 };
long[] uniques = LongArrays.distinct(la);

The above snippet would result in an array containing { 5, 3, 4 }.

Array Sequence Searching

If you want to find the first occurrence of 1 followed by 2 inside an int array you can just use this snippet:

int[] ia = new int[] { 5, 3, 1, 2, 4, 5, 4, 5 };
int index = IntArrays.findSequence(ia, 1, 2);

Array Sequence Counting

Finding out how often 1 followed by 2 occurs inside a short array is really simple.

short[] sa = new short[] { 5, 3, 1, 2, 4, 1, 2, 4, 5 };
int count = ShortArrays.countSequence(sa, (short) 1, (short) 2);

Array Inserting

To insert a char array into another the following code is used.

char[] ca1 = new char[] { 'a', 'd' };
char[] ca2 = new char[] { 'b', 'c' };
char[] abcd = CharArrays.insert(ca1, ca2, 1);

Array Appending

Appending values to the end of a long array has never been easier!

long[] la = new long[] { 1, 2, 3 };
long[] longer = LongArrays.append(la, 4, 5);

Min / Max

You can simply get the smallest short value with the following code.

short[] sa = new short[] { 1, 2, 3 };
short smallest = ShortArrays.min(sa);

To get the highest value you'd just replace min with max.

Average

Calculating an average float value has never been as easy!

float average = FloatArrays.avg(1f, 7f, 7f);

Sum

Summing up a big array of double values can be done like in the next snippet

double[] da = new double[] { 1.4, 2, 3 };
double sum = DoubleArrays.sum(da);

String Reversing

Strings allows you to reverse strings without a big impact on memory usage.

String s = "Hello!";
String reverse = Strings.reverse(s);

The snippet above would result in "!olleH".

String Formatting

A simple way to format strings without a big memory impact or complicated syntax is Strings.simpleFormat.

String world = "World";
char sign = '!';
String greeting = Strings.simpleFormat("Hello {}{}", world, sign);

This would obviously result in "Hello World!".

simpleFormat is approximately 5 times faster than String.format (which to be fair also has a lot more features).

A more in depth test showed that in fact when dealing with a lot of elements (10000+) String.format is the faster implementation.

String Repeating

To repeat a string multiple times simply do the following:

String str = "ab";
String letters = Strings.repeat(4, str);

This would result in "abababab" (the string "ab" repeated 4 times).

License

MIT License

Copyright (c) 2016 Deletescape Media

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

primitives's People

Contributors

nyancrimew avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

primitives's Issues

Try to find better class names

The Pr* naming scheme isn't that beautiful, we should find a new scheme and also check method names.

We can't simply remove the Pr Prefix if we want to avoid problems with java.lang wrapper classes. We also shouldn't just remove it where possible to keep a pattern throughout our api.

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.