Advertisement
zuevv

Nikita is the best Nikita of all Nikitas

Oct 9th, 2020
2,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. package aniskov.ns;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5.  
  6. class NumbersSet {
  7.     private final List<Number> values;
  8.  
  9.     private boolean indexIsValid(int i) {
  10.         return 0 <= i && i < values.size();
  11.     }
  12.  
  13.     public NumbersSet(Number... numbers) {
  14.         values = Arrays.asList(numbers);
  15.     }
  16.  
  17.     public int size() {
  18.         return values.size();
  19.     }
  20.  
  21.     public Number get(int i) {
  22.         return indexIsValid(i) ? values.get(i) : null;
  23.     }
  24.  
  25.     public int set(int i, Number newValue) {
  26.         if (!indexIsValid(i) || newValue.getClass() != this.values.get(i).getClass()) {
  27.             return -1;
  28.         }
  29.         values.set(i, newValue);
  30.         return 0;
  31.     }
  32. }
  33.  
  34. public class Main {
  35.     static void foo(NumbersSet numbersSet) {
  36.         for (int i = 0; i < numbersSet.size(); i++) {
  37.             Number oldValue = numbersSet.get(i);
  38.             System.out.println("---\n Old value: " + oldValue + " (" + oldValue.getClass() + ")");
  39.             for (Number newValue : new Number[]{1, 1L, 1D, 1F, (byte) 1, (short) 1}) {
  40.                 System.out.println("Setting new value: setter returned " + numbersSet.set(i, newValue) + " (" + newValue.getClass() + ")");
  41.             }
  42.         }
  43.     }
  44.  
  45.     public static void main(String[] args) {
  46.         foo(new NumbersSet(1, 1L, 1D, 1F, (byte) 1, (short) 1));
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement