Java Magazine, May/June 2017

Java Magazine, May/June 2017

MAY/JUNE 2017 magazine By and for the Java community Libraries 10 16 34 28 LOMBOK: JDEFERRED’S DATABASE BEST PRACTICES ANNOTATIONS FOR ASYNC EVENT ACCESS WITH FOR LIBRARY CLEANER CODE MANAGEMENT STREAMS DESIGN ORACLE.COM/JAVAMAGAZINE //table of contents / 16 22 28 JDEFERRED: SIMPLE JSOUP HTML DESIGNING AND HANDLING OF PROMISES PARSING LIBRARY IMPLEMENTING AND FUTURES By Mert Çalışkan A LIBRARY By Andrés Almiray Easily parse HTML, extract By Stephen Colebourne Asynchronous operations specified elements, validate The chief designer of Joda- without the headaches structure, and sanitize content. Time lays out best practices for writing your own library. 10 PROJECT LOMBOK: CLEAN, CONCISE CODE By Josh Juneau Add Lombok to your project and get rid of most of your boilerplate code. COVER ART BY PEDRO MURTEIRA 03 34 42 47 From the Editor Databases Fix This User Groups Writing your own annotations? Be Database Actions Using Java 8 By Simon Roberts The Bangladesh JUG circumspect in your design. Stream Syntax Instead of SQL Our latest code quiz 48 05 By Per Minborg 32 Contact Us Events Speedment 3.0 enables Java developers Java Proposals of Interest Have a comment? Suggestion? Want to to stay in Java when writing database Upcoming Java conferences and events JEP 262: Built-in Support for TIFF Files submit an article proposal? Here’s how. applications. 01 ORACLE.COM/JAVAMAGAZINE /////////////////////////////////////////////// MAY/JUNE 2017 EDITORIAL PUBLISHING Editor in Chief Publisher Andrew Binstock Jennifer Hamilton +1.650.506.3794 Managing Editor Associate Publisher and Audience Claire Breen Development Director Copy Editors Karin Kinnear +1.650.506.1985 Karen Perkins, Jim Donahue Audience Development Manager Technical Reviewer Jennifer Kurtz Stephen Chin ADVERTISING SALES DESIGN Sales Director Senior Creative Director Tom Cometa Francisco G Delgadillo Account Manager Design Director Mark Makinney Richard Merchán Mailing-List Rentals Senior Designer Contact your sales representative. Arianna Pucherelli Certified Professional Designer RESOURCES Jaime Ferrand Oracle Products Senior Publication Designer +1.800.367.8674 (US/Canada) Sheila Brennan Oracle Services Production Designer +1.888.283.0591 (US) Kathy Cygnarowicz ARTICLE SUBMISSION If you are interested in submitting an article, please email the editors. SUBSCRIPTION INFORMATION Subscriptions are complimentary for qualified individuals who complete the subscription form. You’ve Earned It MAGAZINE CUSTOMER SERVICE [email protected] Display Your Oracle Certification Digital Badge PRIVACY Oracle Publishing allows sharing of its mailing list with selected third parties. If you prefer that your mailing address or email address not be included in this program, contact Customer Service. Certified Associate Certified Professional Certified Expert Certified Expert Certified Specialist Copyright © 2017, Oracle and/or its affiliates. All Rights Reserved. No part of this publication may be reprinted or otherwise reproduced without permission from the editors. JAVA MAGAZINE IS PROVIDED ON AN “AS IS” BASIS. ORACLE EXPRESSLY DISCLAIMS ALL WARRANTIES, WHETHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ORACLE BE LIABLE FOR ANY DAMAGES OF ANY KIND ARISING FROM YOUR USE OF OR RELIANCE ON ANY INFORMATION PROVIDED HEREIN. Opinions ASSOCIATE PROFESSIONAL EXPERT MASTER SPECIALIST expressed by authors, editors, and interviewees—even if they are Oracle employees—do not necessarily reflect the views of Oracle. The information is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. Oracle and Java are registered trademarks of Oracle Corporation and/or its Claim your certification badge and validate affiliates. Other names may be trademarks of their respective owners. your skills across all online platforms. Java Magazine is published bimonthly and made available at no cost to qualified subscribers by Oracle, 500 Oracle Parkway, MS OPL-3A, Redwood City, CA 94065-1600. 02 ORACLE.COM/JAVAMAGAZINE /////////////////////////////////////////////// MAY/JUNE 2017 //from the editor / Java in the Cloud Keeping Annotations Useful Oracle Cloud delivers When designing annotations, be conservative and circumspect. high-performance and battle-tested platform and infrastructure services for the most demanding n this issue, the lead feature is about a rightly Annotations as they appear in Java itself Java apps. Iwell-regarded library named Project Lombok. follow a similar understated model. Those anno- This library enables you to avoid writing boiler- tations, first unveiled in Java 5, were elegant plate code by using annotations. For example, and concise and didn’t attempt to do too much: Oracle Cloud. add @Data to a class and Lombok will generate the @Override and @Deprecated told you something Built for modern app dev. getters and setters, plus other methods you’re about the code, while @SuppressWarnings told the Built for you. likely to want in a JavaBean-style data class— tools that you knew what you were doing. The toString() and so on. Lombok’s approach is intent was that tools, especially IDEs, would use attractive to me—and to many developers— these markers to issue warnings and reminders. in part because it’s useful, compact, and clear. None of the annotations actually changed pro- In addition, the project includes a tool called gram behavior. This conservative approach by “delombok,” which can remove the annotations the Java language team continued in Java 7, when and insert the boilerplate code into your classes. @SafeVarargs was added, and in Java 8, when In this way, you can easily remove the dependency @FunctionalInterface was delivered. Start here: on Lombok. The annotations are conservative in In addition to the qualities I’ve already men- developer.oracle.com their expression and their roles, and the project tioned, these annotations are unambiguous. This has a clean, reversible implementation. Many more is a key and often overlooked aspect of annota- annotations should follow this approach. tion design. In the quest for brevity, annotation #developersrule PHOTOGRAPH BY BOB ADLER/THE VERBATIM AGENCY 03 ORACLE.COM/JAVAMAGAZINE /////////////////////////////////////////////// MAY/JUNE 2017 //from the editor / authors too often push the respon- As the annotations became com- IntelliJ uses syntax like this: sibility of intelligibility onto plex markers for actions defined @Contract("_, null -> null"). developers. Look no further than elsewhere, you ended up chasing It means that the tagged method the lack of coordination in basic your tail just to determine what the accepts two parameters and syntax. A prime offender here is code you had right before you actu- returns a null if the second param- the family of not-null qualifiers. ally did. This was less than entirely eter is null. Much as I like this @NotNull is such an annotation, fun, which brings me to the second annotation, I feel uncomfortable and so is @NonNull. This annota- problem with many annotations: fully committing to it because it Get a Free tion has a different meaning under insufficient documentation. Unless creates a dependence on the IDE. FindBugs. Likewise, @Nullable the meaning is utterly transparent (Even though another IDE will skip Trial to means different things to the (and even then, as the preceding the annotation it doesn’t recognize, Checker Framework and FindBugs. examples demonstrated), docu- I’ve now left an unused artifact in Oracle Cloud This is not the way to go about ment the annotation thoroughly, my code that might create wasted things. If you author annotations, especially in frameworks. Err on time for downstream developers Experience modern, be clear and definitely avoid syn- the side of overcommunication. trying to understand its function.) open cloud development onyms and homonyms. Finally, I need to stress the In addition, if my whole team is with a free trial to Oracle In comparison to these short, importance of making annota- not using the same IDE, then some Cloud platform and pithy annotations, Java EE intro- tions a sound proposition for the code won’t have these tests and my infrastructure services. duced the extensive use of annota- developer and, by extension, the hope of consistent DbC enforce- tions. Soon, a series of annotations developer’s team. In this regard, ment is either compromised or Get your free trial: replaced code, and the nature of I am leery of IDE vendors’ cre- IDE-dependent. developer.oracle.com enterprise programming thereby ation of their own proprietary Annotations are an important changed. Java EE acquired a sort of annotation systems. All IDEs do part of programming in Java, and embedded syntax that straddled this to some extent, but I’ll pick their role is likely to expand. But descriptions and commands. The an example from the one I use new annotations should be devised advent of this style reinvigorated most. IntelliJ IDEA uses annota- with far greater circumspection Java EE by making it far easier tions to deliver a minimal but than in the past, named with care- to code and by getting rid of the clever implementation of design ful attention to predecessors, and heaviness of its forebears. by contract (DbC)–style enforce- documented

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    49 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us