@Getter
private List<String> names;
private List<String> names;
public List<String> getNames() {
if (names == null) {
names = new ArrayList<>();
}
return names;
}
> To unsubscribe from this group and stop receiving emails from it, send an email to project...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/project-lombok/68432585-34c6-4537-89e9-6f5b670162d4%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/project-lombok/68432585-34c6-4537-89e9-6f5b670162d4%40googlegroups.com.
names.size()
names.add("Jon");
> To view this discussion on the web visit https://groups.google.com/d/msgid/project-lombok/c5d287d2-9a97-44d5-ac68-4fa45c947ed6%40googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/project-lombok/7fca7169-082b-419d-a924-fb4403ae4c73%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/project-lombok/CAOu%2BafnEv_o1uqKuUA-8rnHqHvpGHtY7%3D%2BAJpMcQD38fCs7HLw%40mail.gmail.com.
> To unsubscribe from this group and stop receiving emails from it, send an email to project-lombok+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/project-lombok/c5d287d2-9a97-44d5-ac68-4fa45c947ed6%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombok+unsubscribe@googlegroups.com.
The overhead getterlazy introduces is required for the operation to be threadsafe.Making a 'thread unsafe lazy getter', especially one where null has special meaning (with the current impl of lazygetter, an expensive expression that produces null is still 'cached', if you want maximum cheapness, we can say that null is the same as 'to be calculated', effectively) – would indeed be faster.
But here's the unfortunate truth: That is either sucky code, or more likely a misjudgement of priorities, where programmers are wasting developer time thinking of irrelevant things like 'maybe this is more efficient if I lazily init this list'.
> To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/project-lombok/c5d287d2-9a97-44d5-ac68-4fa45c947ed6%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/project-lombok/CAOu%2BafnEv_o1uqKuUA-8rnHqHvpGHtY7%3D%2BAJpMcQD38fCs7HLw%40mail.gmail.com.
--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/project-lombok/3e00a662-922d-4080-828b-81e1ce25157a%40googlegroups.com.
On Mon, Feb 17, 2020 at 7:05 PM Reinier Zwitserloot <rein...@gmail.com> wrote:Just to make sure, we understand each other:- I was just trying to interpret the original request so it makes an useful feature
public class ClassWithLists{
private List<String> strings;
private List<Integer> numbers;
private <T> List<T> lister(List<T> list){
return list == null ? new ArrayList<>() : list;
}
public List<String> getStrings(){
return lister(this.strings);
}
public void setStrings(List<String> list){
this.strings = lister(list);
}
... (other stuff)
}
@Data
public class ClassWithLists{
@NeverNull(ArrayList.class)
private List<String> strings;
@NeverNull(LinkedList.class)
private List<Integer> numbers;
... (other stuff)
}
public static void main(String[] args){
ClassWithLists cwl = new ClassWithLists();
System.out.println(cwl.getStrings().size()); // "0"
cwl.setStrings(null); // void
System.out.println(cwl.getStrings().add("Test"));// "True" (successfully added to whatever list was returned by cwl.getStrings
System.out.println(cwl.getStrings().size()); // "1" (shows that the list returned was actually
List<Strings> ls = cwl.getStrings(); //
ls = null; // This is just to show that in java, this doesn't behave like a pointer and change the list inside cwl object
System.out.println(cwl.getStrings().size()); // This is the verification the the previous line didn't actually change the object, and that it still has 1 String in it
}
null
arguments and return values cannot be reliably distinguished from the absence of elements." https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html -> from this same logic, when someone passes a value of null to the setter, the API is making a clearly documented assumption, that null can be used to signal something else. The reason it can be used to signal something else is because Java allows methods to be called with null as an argument. Even though we don't want to accept it, our options are either, throw an exception, or handle it some way. The API as I've outlined, says that null will be used to scrap the current list and start a new one. Other valid options include a no-op, so that nothing will happen, or a typical setter, in which the list would be set to null (this is the specific scenario I'm trying to avoid), or clearing the existing list, but using the same reference.public class OtherClassWithLists {
private List<String> strings;
private List<Integer> numbers;
public OtherClassWithLists() {
strings = new ArrayList<>();
}
public List<String> getStrings(){
return this.strings;
}
public void setStrings(List<String> list){
if(list == null)
strings.clear();
else
strings = list;
}
public static void main(String[] args) {
OtherClassWithLists cwl = new OtherClassWithLists();
System.out.println(cwl.getStrings().size()); // "0"
cwl.setStrings(null); // void
System.out.println(cwl.getStrings().add("Test"));// "true" (successfully added to whatever list was returned by cwl.getStrings
System.out.println(cwl.getStrings().size()); // "1" (shows that the list returned was actually
List<String> ls = cwl.getStrings(); //
ls = null; // This is just to show that in java, this doesn't behave like a pointer and change the list inside cwl object
System.out.println(cwl.getStrings().size()); // This is the verification the the previous line didn't actually change the object, and that it still has 1 String in it
List<String> list1 = cwl.getStrings();
cwl.setStrings(null);
System.out.println(cwl.getStrings().size()); // "0" null = clear list
System.out.println(list1==cwl.getStrings()); // "true" - they are the same reference
}
}
As programmers, we get to decide which values are valid arguments. For example, the Java Collections API says this when talking about concurrent collection implementations and why null values aren't allowed: "null
arguments and return values cannot be reliably distinguished from the absence of elements." https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html
-> from this same logic, when someone passes a value of null to the setter, the API is making a clearly documented assumption, that null can be used to signal something else. The reason it can be used to signal something else is because Java allows methods to be called with null as an argument. Even though we don't want to accept it, our options are either, throw an exception, or handle it some way. The API as I've outlined, says that null will be used to scrap the current list and start a new one. Other valid options include a no-op, so that nothing will happen, or a typical setter, in which the list would be set to null (this is the specific scenario I'm trying to avoid), or clearing the existing list, but using the same reference.
As programmers, we get to decide which values are valid arguments.