Ana səhifə

GetterSetterExample


Yüklə 74 Kb.
tarix25.06.2016
ölçüsü74 Kb.
@Getter/Setter

01 import lombok.AccessLevel;

02 import lombok.Getter;

03 import lombok.Setter;

04


05 public class GetterSetterExample {

06 @Getter @Setter private int age = 10;

07 @Setter(AccessLevel.PROTECTED) private String name;

08


09 @Override public String toString() {

10 return String.format("%s (age: %d)", name, age);

11 }

12 }


01 public class GetterSetterExample {

02 private int age = 10;

03 private String name;
04

05 @Override public String toString() {

06 return String.format("%s (age: %d)", name, age);

07 }


08

09 public int getAge() {

10 return age;

11 }


12

13 public void setAge(int age) {

14 this.age = age;

15 }


16

17 protected void setName(String name) {

18 this.name = name;

19 }


20 }


@Getter(lazy=true)

01 import lombok.Getter;

02


03 public class GetterLazyExample {

04 @Getter(lazy=true) private final double[] cached = expensive();

05

06 private double[] expensive() {



07 double[] result = new double[1000000];

08 for (int i = 0; i < result.length; i++) {

09 result[i] = Math.asin(i);

10 }


11 return result;

12 }


13 }

01 public class GetterLazyExample {

02 private double[] $lombok$lazy1v;

03 private volatile boolean $lombok$lazy1i;

04 private final Object $lombok$lazyLock = new Object[0];

05


06 public double[] getCached() {

07 if (!this.$lombok$lazy1i) {

08 synchronized (this.$lombok$lazyLock) {

09 if (!this.$lombok$lazy1i) {

10 this.$lombok$lazy1v = expensive();

11 this.$lombok$lazy1i = true;

12 }

13 }


14 }

15 return this.$lombok$lazy1v;

16 }

17


18 private double[] expensive() {

19 double[] result = new double[1000000];

20 for (int i = 0; i < result.length; i++) {

21 result[i] = Math.asin(i);

22 }

23 return result;



24 }

25 }


@ToString

import lombok.ToString;

02


03 @ToString(exclude="id")

04 public class ToStringExample {

05 private static final int STATIC_VAR = 10;

06 private String name;

07 private Shape shape = new Square(5, 10);

08 private String[] tags;

09 private int id;

10


11 public String getName() {

12 return this.getName();

13 }

14


15 @ToString(callSuper=true, includeFieldNames=true)

16 public static class Square extends Shape {

17 private final int width, height;

18


19 public Square(int width, int height) {

20 this.width = width;

21 this.height = height;

22 }


23 }

24 }


01 import java.util.Arrays;

02
03 public class ToStringExample {

04 private static final int STATIC_VAR = 10;

05 private String name;

06 private Shape shape = new Square(5, 10);

07 private String[] tags;

08 private int id;

09


10 public String getName() {

11 return this.getName();

12 }

13


14 public static class Square extends Shape {

15 private final int width, height;

16

17 public Square(int width, int height) {



18 this.width = width;

19 this.height = height;

20 }

21


22 @Override public String toString() {

23 return "Square(super=" + super.toString() + ", width=" + this.width + ", height=" + this.height + ")";

24 }

25 }


26

27 @Override public String toString() {

28 return "ToStringExample(" + this.getName() + ", " + this.shape + ", " + Arrays.deepToString(this.tags) + ")";

29 }


30 }

val

01 import java.util.ArrayList;

02 import java.util.HashMap;

03 import lombok.val;

04


05 public class ValExample {

06 public String example() {

07 val example = new ArrayList();
08 example.add("Hello, World!");

09 val foo = example.get(0);

10 return foo.toLowerCase();

11 }


12

13 public void example2() {

14 val map = new HashMap();
15 map.put(0, "zero");

16 map.put(5, "five");

17 for (val entry : map.entrySet()) {
18 System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());

19 }


20 }

21 }


01 import java.util.ArrayList;

02 import java.util.HashMap;

03 import java.util.Map;

04


05 public class ValExample {

06 public String example() {

07 final ArrayList example = new ArrayList();

08 example.add("Hello, World!");

09 final String foo = example.get(0);

10 return foo.toLowerCase();

11 }

12


13 public void example2() {

14 final HashMap map = new HashMap();

15 map.put(0, "zero");

16 map.put(5, "five");

17 for (final Map.Entry entry : map.entrySet()) {

18 System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());

19 }

20 }


21 }

@EqualsAndHashCode

01 import lombok.EqualsAndHashCode;

02


03 @EqualsAndHashCode(exclude={"id", "shape"})

04 public class EqualsAndHashCodeExample {

05 private transient int transientVar = 10;

06 private String name;

07 private double score;

08 private Shape shape = new Square(5, 10);

09 private String[] tags;

10 private int id;

11

12 public String getName() {



13 return this.name;

14 }


15

16 @EqualsAndHashCode(callSuper=true)

17 public static class Square extends Shape {

18 private final int width, height;

19

20 public Square(int width, int height) {



21 this.width = width;

22 this.height = height;

23 }

24 }


25 }

01 import java.util.Arrays;

02
03 public class EqualsAndHashCodeExample {

04 private transient int transientVar = 10;

05 private String name;

06 private double score;

07 private Shape shape = new Square(5, 10);

08 private String[] tags;

09 private int id;

10

11 public String getName() {



12 return this.name;

13 }


14

15 @Override public boolean equals(Object o) {

16 if (o == this) return true;

17 if (o == null) return false;

18 if (!(o instanceof EqualsAndHashCodeExample)) return false;

19 EqualsAndHashCodeExample other = (EqualsAndHashCodeExample) o;

20 if (!other.canEqual(this)) return false;

21 if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;

22 if (Double.compare(this.score, other.score) != 0) return false;

23 if (!Arrays.deepEquals(this.tags, other.tags)) return false;

24 return true;

25 }


26

27 @Override public int hashCode() {

28 final int PRIME = 31;

29 int result = 1;

30 final long temp1 = Double.doubleToLongBits(this.score);

31 result = (result*PRIME) + (this.name == null ? 0 : this.name.hashCode());

32 result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32));

33 result = (result*PRIME) + Arrays.deepHashCode(this.tags);

34 return result;

35 }


36

37 public boolean canEqual(Object other) {

38 return other instanceof EqualsAndHashCodeExample;

39 }


40

41 public static class Square extends Shape {

42 private final int width, height;

43


44 public Square(int width, int height) {

45 this.width = width;

46 this.height = height;

47 }


48

49 @Override public boolean equals(Object o) {

50 if (o == this) return true;

51 if (o == null) return false;

52 if (!(o instanceof Square)) return false;

53 Square other = (Square) o;

54 if (!other.canEqual(this)) return false;

55 if (!super.equals(o)) return false;

56 if (this.width != other.width) return false;

57 if (this.height != other.height) return false;

58 return true;

59 }


60

61 @Override public int hashCode() {

62 final int PRIME = 31;

63 int result = 1;

64 result = (result*PRIME) + super.hashCode();

65 result = (result*PRIME) + this.width;

66 result = (result*PRIME) + this.height;

67 return result;

68 }

69


70 public boolean canEqual(Object other) {

71 return other instanceof Square;

72 }

73 }


74 }

@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor

01 import lombok.AccessLevel;

02 import lombok.RequiredArgsConstructor;

03 import lombok.AllArgsConstructor;

04 import lombok.NonNull;

05

06 @RequiredArgsConstructor(staticName = "of")



07 @AllArgsConstructor(access = AccessLevel.PROTECTED)

08 public class ConstructorExample {

09 private int x, y;

10 @NonNull private T description;

11

12 @NoArgsConstructor



13 public static class NoArgsExample {

14 @NonNull private String field;


15 }


16 }

01 public class ConstructorExample {

02 private int x, y;

03 @NonNull private T description;

04


05 private ConstructorExample(T description) {

06 if (description == null) throw new NullPointerException("description");

07 this.description = description;

08 }


09

10 public static ConstructorExample of(T description) {

11 return new ConstructorExample(description);

12 }


13

14 @java.beans.ConstructorProperties({"x", "y", "description"})

15 protected ConstructorExample(int x, int y, T description) {

16 if (description == null) throw new NullPointerException("description");

17 this.x = x;

18 this.y = y;

19 this.description = description;

20 }


21

22 public static class NoArgsExample {

23 @NonNull private String field;

24


25 public NoArgsExample() {

26 }


27 }

28 }


@Cleanup

01 import lombok.Cleanup;

02 import java.io.*;

03

04 public class CleanupExample {



05 public static void main(String[] args) throws IOException {

06 @Cleanup InputStream in = new FileInputStream(args[0]);

07 @Cleanup OutputStream out = new FileOutputStream(args[1]);

08 byte[] b = new byte[10000];

09 while (true) {

10 int r = in.read(b);

11 if (r == -1) break;

12 out.write(b, 0, r);

13 }

14 }


15 }

01 import java.io.*;
02

03 public class CleanupExample {

04 public static void main(String[] args) throws IOException {

05 InputStream in = new FileInputStream(args[0]);

06 try {

07 OutputStream out = new FileOutputStream(args[1]);

08 try {

09 byte[] b = new byte[10000];

10 while (true) {

11 int r = in.read(b);

12 if (r == -1) break;

13 out.write(b, 0, r);

14 }

15 } finally {



16 if (out != null) {

17 out.close();

18 }

19 }


20 } finally {

21 if (in != null) {

22 in.close();

23 }


24 }

25 }


26 }

@Synchronized

01 import lombok.Synchronized;

02


03 public class SynchronizedExample {

04 private final Object readLock = new Object();

05

06 @Synchronized



07 public static void hello() {
08 System.out.println("world");

09 }


10

11 @Synchronized

12 public int answerToLife() {
13 return 42;

14 }


15

16 @Synchronized("readLock")

17 public void foo() {
18 System.out.println("bar");

19 }
20 }



01 public class SynchronizedExample {

02 private static final Object $LOCK = new Object[0];

03 private final Object $lock = new Object[0];

04 private final Object readLock = new Object();

05
06 public static void hello() {

07 synchronized($LOCK) {

08 System.out.println("world");

09 }

10 }


11

12 public int answerToLife() {

13 synchronized($lock) {

14 return 42;

15 }

16 }


17

18 public void foo() {

19 synchronized(readLock) {

20 System.out.println("bar");

21 }

22 }


23 }

@SneakyThrows

01 import lombok.SneakyThrows;

02


03 public class SneakyThrowsExample implements Runnable {

04 @SneakyThrows(UnsupportedEncodingException.class)

05 public String utf8ToString(byte[] bytes) {
06 return new String(bytes, "UTF-8");

07 }


08

09 @SneakyThrows

10 public void run() {

11 throw new Throwable();

12 }

13 }


01 import lombok.Lombok;

02


03 public class SneakyThrowsExample implements Runnable {

04 public String utf8ToString(byte[] bytes) {

05 try {

06 return new String(bytes, "UTF-8");

07 } catch (UnsupportedEncodingException e) {

08 throw Lombok.sneakyThrow(e);

09 }

10 }


11
12 public void run() {

13 try {


14 throw new Throwable();

15 } catch (Throwable t) {

16 throw Lombok.sneakyThrow(t);

17 }


18 }

19 }


@Log

01 import lombok.extern.slf4j.Log;

02


03 @Log

04 public class LogExample {

05

06 public static void main(String... args) {



07 log.error("Something's wrong here");

08 }


09 }

10


11 @Log(java.util.List.class)

12 public class LogExampleOther {

13

14 public static void main(String... args) {



15 log.warn("Something might be wrong here");

16 }


17 }

01 public class LogExample {

02 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);

03


04 public static void main(String... args) {
05 log.error("Something's wrong here");

06 }


07 }

08
09 public class LogExampleOther {

10 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(java.util.List.class);

11


12 public static void main(String... args) {
13 log.warn("Something might be wrong here");
14 }

15 }


@Delegate

01 import java.util.ArrayList;

02 import java.util.Collection;

03

04 import lombok.Delegate;



05

06 public class DelegationExample {

07 private interface SimpleCollection {

08 boolean add(String item);

09 boolean remove(Object item);

10 }


11

12 @Delegate(types=SimpleCollection.class)

13 private final Collection collection = new ArrayList();

14 }


16

17 class ExcludesDelegateExample {

18 long counter = 0L;

19


20 private interface Add {

21 boolean add(String x);

22 boolean addAll(Collection x);

23 }


24

25 @Delegate(excludes=Add.class)

26 private final Collection collection = new ArrayList();

27


28 public boolean add(String item) {

29 counter++;

30 return collection.add(item);

31 }


32

33 public boolean addAll(Collection col) {

34 counter += col.size();

35 return collection.addAll(col);

36 }

37 }


01 import java.util.ArrayList;

02 import java.util.Collection;

03 import lombok.Delegate;

04
05 public class DelegationExample {

06 private interface SimpleCollection {

07 boolean add(String item);

08 boolean remove(Object item);

09 }


10
11 private final Collection collection = new ArrayList();

12


13 @java.lang.SuppressWarnings("all")

14 public boolean add(final java.lang.String item) {

15 return this.collection.add(item);

16 }


17

18 @java.lang.SuppressWarnings("all")

19 public boolean remove(final java.lang.Object item) {

20 return this.collection.remove(item);

21 }

22 }


23

24 class ExcludesDelegateExample {

25 long counter = 0L;

26


27 private interface Add {

28 boolean add(String x);

29 boolean addAll(Collection x);

30 }


31

32 private final Collection collection = new ArrayList();

33
34 public boolean add(String item) {

35 counter++;

36 return collection.add(item);

37 }


38

39 public boolean addAll(Collection col) {

40 counter += col.size();

41 return collection.addAll(col);

42 }

43


44 @java.lang.SuppressWarnings("all")

45 public int size() {

46 return this.collection.size();

47 }


48

49 @java.lang.SuppressWarnings("all")

50 public boolean isEmpty() {

51 return this.collection.isEmpty();

52 }

53


54 @java.lang.SuppressWarnings("all")

55 public boolean contains(final java.lang.Object arg0) {

56 return this.collection.contains(arg0);

57 }


58

59 @java.lang.SuppressWarnings("all")

60 public java.util.Iterator iterator() {

61 return this.collection.iterator();

62 }

63


64 @java.lang.SuppressWarnings("all")

65 public java.lang.Object[] toArray() {

66 return this.collection.toArray();

67 }


68

69 @java.lang.SuppressWarnings("all")

70 public T[] toArray(final T[] arg0) {

71 return this.collection.toArray(arg0);

72 }

73


74 @java.lang.SuppressWarnings("all")

75 public boolean remove(final java.lang.Object arg0) {

76 return this.collection.remove(arg0);

77 }


78

79 @java.lang.SuppressWarnings("all")

80 public boolean containsAll(final java.util.Collection arg0) {

81 return this.collection.containsAll(arg0);

82 }

83


84 @java.lang.SuppressWarnings("all")

85 public boolean removeAll(final java.util.Collection arg0) {

86 return this.collection.removeAll(arg0);

87 }


88

89 @java.lang.SuppressWarnings("all")

90 public boolean retainAll(final java.util.Collection arg0) {

91 return this.collection.retainAll(arg0);

92 }

93


94 @java.lang.SuppressWarnings("all")

95 public void clear() {

96 this.collection.clear();

97 }


98 }


Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©atelim.com 2016
rəhbərliyinə müraciət