Giter Site home page Giter Site logo

java9-objects-new-methods's Introduction

Build Status

java9-objects-new-methods

Java9: Overview of new methods in the Objects class.

preface

In Java 9, Objects class was extended with new static methods:

  • T requireNonNullElse(T obj, T defaultObj) -
    • Returns the first argument if it is non-null and
    • otherwise returns the non-null second argument,
    • otherwise NPE.
    public static <T> T requireNonNullElse(T obj, T defaultObj) {
        return (obj != null) ? obj : requireNonNull(defaultObj, "defaultObj");
    }
    
  • T requireNonNullElseGet(T obj, Supplier<? extends T> supplier) -
    • Returns the first argument if it is non-null
    • otherwise returns the non-null value of supplier.get(),
    • otherwise NPE.
    public static <T> T requireNonNullElseGet(T obj, Supplier<? extends T> supplier) {
        return (obj != null) ? obj
                : requireNonNull(requireNonNull(supplier, "supplier").get(), "supplier.get()");
    }
    
  • int checkIndex(int index, int length) - Checks if index e [0; length).
    • returns index if it is within bounds of the range
  • int checkFromToIndex(int fromIndex, int toIndex, int length) - Checks if [fromIndex; toIndex) c [0; length).
    • returns fromIndex if the sub-range within bounds of the range
  • int checkFromIndexSize(int fromIndex, int size, int length) - Checks if [fromIndex; fromIndex + size) c [0; length).
    • returns fromIndex if the sub-range within bounds of the range

project description

We provide basics tests of above mentioned methods. Below we list the most interesting cases

  • checkIndex
    • checkIndex(0, 0)
      @Test(expected = IndexOutOfBoundsException.class)
      public void checkIndex_index0_length0() {
          Objects.checkIndex(0, 0);
      }
      
    • checkIndex(3, 6)
      assertThat(Objects.checkIndex(3, 6), is(3));
      
  • checkFromToIndex
    assertThat(Objects.checkFromToIndex(0, 3, 3), is(0));
    
  • checkFromIndexSize
    @Test(expected = IndexOutOfBoundsException.class)
    public void checkFromIndexSize_size_lessThan0() {
        Objects.checkFromIndexSize(5, -3, 10);
    }
    

java9-objects-new-methods's People

Contributors

mtumilowicz avatar

Watchers

James Cloos 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.