Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts
Guide to Google AutoValue
AutoValue runs inside javac as a standard annotation processor.
Use Builder when there too many parameters or same type.
@AutoValue
public abstract class Foo {
public static Foo create(String text, int number) {
// defensive copies, preconditions
return new AutoValue_Foo(text, number);
}
public abstract String text(); // or getText(), if you like
public abstract int number();
}
Set a default value for a property
static Builder builder() {
return new AutoValue_Foo.Builder()
.setSomeIntegerField(1).setSomeImmutableSetFeild(ImmutableSet.of());
}
@Nullable properties
toBuilder
AutoValue.Builder
@AutoValue
public abstract class Foo {
static Builder builder() {
return new AutoValue_Foo.Builder();
}
public abstract String text(); // or getText(), if you like
public abstract int number();
@AutoValue.Builder
abstract static class Builder {
abstract Builder text(String value);
abstract Builder number(int value);
abstract Foo build();
}
}
Optional properties
public abstract Optional<String> optionalProperty();
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder SetOptionalProperty(String optionalProperty);
}
autoBuild
@AutoValue.Builder
public abstract static class Builder {
abstract Animal autoBuild();
public Animal build() {
// we can do validation, or normalization, or any other logic here.
return autoBuild();
}
}
Misc
- Use immutable properties.
- We can perform validations at create factory or build method.
- We can write our own equals or hashcode methods.
Resource
Eclipse vs IntelliJ
Better ways to organize info - Winner: Eclipse
- Eclipse provides workspace, working set, projects
- Organizes views into perspectives, provides view like console, problems, errors.
- We always know where to find info we need.
Editor -Winner: Eclipse
Eclipse shows error/warning in current window in left side bar.
IntelliJ only shows error/warning for all content in right side bar. (Eclipse also shows things in right side bar)
When you write code, its much easier to check error/warning in Eclipse.
Shortcut keys -Winner: Eclipse
Eclipse's default shortcut is more natural to me: easier to remember and type. Usually Eclipse uses less keys. IntelliJ uses all keys F1-10.
To format code: Cmd+shift+f in eclipse Cmd+alt+l in IntelliJ
- what L means?
Quick fix - Winner: Eclipse
- Eclipse quick fix(ctrl+1) is too powerful, it can fix bugs, generate code based on current context.
- IntelliJ provides too many ways to do similar things - wtf?
Show intention actions - alt+enter
Basic code completion - ctrl+space
Smart type code completion - ctrl+shift+space
Debug
- Drop to frame always works in Eclipse, but doesn't in IntelliJ
- Display view in Eclipse stores history, it's much easier to use
IntelliJ does have its own advantages such as: smart step into for anonymous classes and lambdas.
Search everywhere in IntelliJ
In Eclipse, you have to use different shortcuts/ways to search type, resource, views.
In IntelliJ, just double press shift keys.
Better Performance/More Stable - Winner: IntelliJ
Sometimes Eclipse is slow or gets stuck, you need take time to customize it, check Improve Eclipse Performance
Same code may work in Eclipse but not in IntelliJ
Eclipse and IntelliJ doesn't works well together, if some team members are using eclipse, you better also use eclipse, as sometimes, the same code works and can be deployed in eclipse's tomcat but doesn't work or can't be deployed in IntelliJ tomcat.
More about Eclipse and IntelliJ:
Mastering Eclipse IDE - Tips, Tricks
IntelliJ IDEA Tips and Tricks
- Eclipse provides workspace, working set, projects
- Organizes views into perspectives, provides view like console, problems, errors.
- We always know where to find info we need.
Editor -Winner: Eclipse
Eclipse shows error/warning in current window in left side bar.
IntelliJ only shows error/warning for all content in right side bar. (Eclipse also shows things in right side bar)
When you write code, its much easier to check error/warning in Eclipse.
Shortcut keys -Winner: Eclipse
Eclipse's default shortcut is more natural to me: easier to remember and type. Usually Eclipse uses less keys. IntelliJ uses all keys F1-10.
To format code: Cmd+shift+f in eclipse Cmd+alt+l in IntelliJ
- what L means?
Quick fix - Winner: Eclipse
- Eclipse quick fix(ctrl+1) is too powerful, it can fix bugs, generate code based on current context.
- IntelliJ provides too many ways to do similar things - wtf?
Show intention actions - alt+enter
Basic code completion - ctrl+space
Smart type code completion - ctrl+shift+space
Debug
- Drop to frame always works in Eclipse, but doesn't in IntelliJ
- Display view in Eclipse stores history, it's much easier to use
IntelliJ does have its own advantages such as: smart step into for anonymous classes and lambdas.
Search everywhere in IntelliJ
In Eclipse, you have to use different shortcuts/ways to search type, resource, views.
In IntelliJ, just double press shift keys.
Better Performance/More Stable - Winner: IntelliJ
Sometimes Eclipse is slow or gets stuck, you need take time to customize it, check Improve Eclipse Performance
Same code may work in Eclipse but not in IntelliJ
Eclipse and IntelliJ doesn't works well together, if some team members are using eclipse, you better also use eclipse, as sometimes, the same code works and can be deployed in eclipse's tomcat but doesn't work or can't be deployed in IntelliJ tomcat.
More about Eclipse and IntelliJ:
Mastering Eclipse IDE - Tips, Tricks
IntelliJ IDEA Tips and Tricks
Subscribe to:
Posts (Atom)
Labels
ANT
(6)
Algorithm
(69)
Algorithm Series
(35)
Android
(7)
Big Data
(7)
Blogger
(14)
Bugs
(6)
Cache
(5)
Chrome
(19)
Code Example
(29)
Code Quality
(7)
Coding Skills
(5)
Database
(7)
Debug
(16)
Design
(5)
Dev Tips
(63)
Eclipse
(32)
Git
(5)
Google
(33)
Guava
(7)
How to
(9)
Http Client
(8)
IDE
(7)
Interview
(88)
J2EE
(13)
J2SE
(49)
JSON
(7)
Java
(186)
JavaScript
(27)
Learning code
(9)
Lesson Learned
(6)
Linux
(26)
Lucene-Solr
(112)
Mac
(10)
Maven
(8)
Network
(9)
Nutch2
(18)
Performance
(9)
PowerShell
(11)
Problem Solving
(11)
Programmer Skills
(6)
Scala
(6)
Security
(9)
Soft Skills
(38)
Spring
(22)
System Design
(11)
Testing
(7)
Text Mining
(14)
Tips
(17)
Tools
(24)
Troubleshooting
(29)
UIMA
(9)
Web Development
(19)
Windows
(21)
adsense
(5)
bat
(8)
regex
(5)
xml
(5)