Maven and Java Generics - cvut.cz · PDF fileMaven and Java Generics Jan Jusko...
Transcript of Maven and Java Generics - cvut.cz · PDF fileMaven and Java Generics Jan Jusko...
Maven and Java Generics Jan Jusko [email protected] 24.2.2014
2 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Goal
1. Establish java project format 2. Show use of Generics in Java
3 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Java Project Structure
4 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Use Maven
• Automation tool for java projects • Makes dependency management easier • Common build workflow • And many more! Makes our job easier!
6 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Dependency Management
• Need to import IDE project manually • Different IDEs • Very labor intensive for 50+ projects
7 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Dependency Management with Maven
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version>
</dependency>
8 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
How to Work with Maven
• Maven installed • Win: download, unpack & add to path • Lin: aptitude install maven3 • OSX: same same but different
• Predefined directory structure • Configuration file (pom.xml)
9 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Maven Directory structure
10 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Using Maven
• Compile $ mvn compile • Run tests $ mvn test • Create jar $ mvn package
12 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Motivation
• Type checking @ compile-time • Limit explicit casting • Implementation of generic algorithms
• You probably encountered them in java Collections
13 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Generics in Collections
Set varSet = new HashSet(); • may contain any object type • notice the interface!
Set<String> varSet = new HashSet<>(); • may contain only instances of String
14 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Raw Collections
• No compile-time check • Responsibility of programmer • Runtime Exceptions
15 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Generic Collections
• Compiler checks for type safety
16 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Arrays Can Be Generic As Well
17 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Generic Arrays vs. Generic Collections
18 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Invariance of Generic Collections
• Integer extends Number • Collection<Integer> does not extend Collection<Number>
19 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Variance of Generic Arrays
• Integer extends Number • Integer[] extends Number[]
20 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Generic Arrays vs. Generic Collections
21 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
You Cannot Create Generic Arrays
23 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Wildcards with Generics
• Sometimes you don’t care what type is in Collection • Then you can use a wildcard <?>
25 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Pure Wildcard
• Collections with pure wildcard are effectively “read only”
• Only null can be added to the collection • For methods that do not care about contents of
collection
26 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Extending Wildcard
• Also effectively “read only”, with null only possible addition
• For methods that can work with base class or any other extending class
27 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Extending Wildcard as Method Parameter
• Since Generic Collections Are Invariant
28 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Extending Wildcard vs. Superclass
• Collection of Superclass contains any instance of extending classes
• Collection of Extending Wildcard contains elements of specific extending class
29 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Super Wildcard
• Can add any instance of Number or instance of its predecessor
• Can fetch only Object • Not very common
30 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Creating Generic Classes
31 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Creating Restricted Generic Classes
32 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Creating Restricted Generic Classes
33 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Recursive Generic Parameter
35 © 2014 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Generic Class & Methods