Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.jar
*.war
*.ear
.DS_Store

# Exception for gradle-wrapper
!tools/gradlew-simplejava/wrapper.jar
2 changes: 1 addition & 1 deletion contributions/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ subprojects {
apply plugin: 'eclipse'

// If we don't set this, Eclipse will get confused
sourceCompatibility = '1.7'
sourceCompatibility = '10'

// Use the maven repository to fetch dependencies
repositories {
Expand Down
4 changes: 4 additions & 0 deletions contributions/jaxbComposition/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ version = '1.0.0'

// Implementation specifics
// This implementation uses JUnit for testing
// Added JAXB Dependencies, which are deprecated in JDK9+
dependencies {
compile('javax.activation:activation:1.1')
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.0-b180830.0359'
compile group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: '2.4.0-b180830.0438'
testCompile group: 'junit', name: 'junit', version: '4.11+'
}
// Generate model classes with xjc
Expand Down
102 changes: 102 additions & 0 deletions contributions/jaxbCompositionJava10/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
== Headline ==

[[Object-XML mapping]] with [[Technology:JAXB|]] and [Language:Java|]]8 [[Namespace:Feature]]s

== Characteristics ==

For the overall motivation of exercising [[Technology:JAXB]] see [[Contribution:jaxbComposition]]. This contribution is an overhaul of [[Contribution:jaxbComposition]] by applying new [[Namespace:Feature]]s introduced with [[Language:Java 8|]] such as [[Feature:Lambda Expressions]], [[Feature:Method references]] and [[Feature:Streams]]. The general idea behind this modernization is to update all necessary project dependencies, improve code readability and represent code in a more functional fashion by applying the aforementioned features.

== Illustration ==

[[Feature:Open serialization]] is implemented using [[Technology:JAXB]] Un-/Marshaller. Furthermore the code has been reduced by employing Method chaining:

<fragment url="src/main/java/org/softlang/company/features/Serialization.java/class/Serialization/method/deserializeCompany"/>

public static Company deserializeCompany(File input)
throws JAXBException
{
initializeJaxbContext();
return (Company) jaxbContext.createUnmarshaller().unmarshal(input);
}

<fragment url="src/main/java/org/softlang/company/features/Serialization.java/class/Serialization/method/serializeCompany"/>


public static void serializeCompany(File output, Company c)
throws JAXBException,
FileNotFoundException,
XMLStreamException
{
initializeJaxbContext();
XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileOutputStream(output));
jaxbContext.createMarshaller().marshal(c, writer);
}

[[Feature:Total]] features [[Feature:Streams]] as a way of processing elements and [[Feature:Cut]] relies on Optionals for dealing with possible null values:

<fragment url="src/main/java/org/softlang/company/features/Total.java/class/Total"/>

public static double total(Department department) {

double total = Optional.ofNullable(department.getManager()).map(Employee::getSalary).orElse(0.0);

total += department.getDepartment()
.stream()
.map(dep -> total(dep))
.reduce(0.0, Double::sum);

return total += department.getEmployee()
.stream()
.map(emp -> emp.getSalary())
.reduce(0.0, Double::sum);
}


<fragment url="src/main/java/org/softlang/company/features/Cut.java/class/Cut"/>

public static void cut(Company company) {
Optional.ofNullable(company)
.map(Company::getDepartment)
.ifPresent(depts -> depts.forEach(Cut::cut));
}

Test cases are implemented for all [[Namespace:Feature]]s.

== Relationships ==

For Object/XML mapping see [[Contribution:jaxbChoice]] (XSD with choice for different subunits), [[Contribution:jaxbComposition]] (XSD with object composition), [[Contribution:jaxbExtension]] (XSD with type extension) and [[Contribution:jaxbSubstitution]] (XSD with substitution groups).

== Architecture ==

The contribution follows a standardized structure:
* inputs contains input files for tests
* src/main/java contains the following packages:
** org.softlang.company.features for implementations of [[Functional requirements]].
* src/test/java contains the following packages:
** org.softlang.company.tests for [[Technology:JUnit]] test cases for [[Namespace:Feature]]s.

== Usage ==

This contribution uses [[Technology:Gradle]] for building. [[Technology:Eclipse]] is supported.

See https://github.com/101companies/101simplejava/blob/master/README.md

== Metadata ==

* [[implements::Feature:Hierarchical company]]
* [[implements::Feature:Mapping]]
* [[implements::Feature:Open serialization]]
* [[implements::Feature:Total]]
* [[implements::Feature:Cut]]
* [[memberOf::Theme:Java mapping]]
* [[memberOf::Theme:XML programming]]
* [[uses::Language:Java]]
* [[uses::Language:XML]]
* [[uses::Language:XSD]]
* [[uses::Technology:JAXB]]
* [[uses::Technology:xjc]]
* [[uses::Technology:JUnit]]
* [[uses::Technology:Gradle]]
* [[uses::Concept:Lambdaware]]


35 changes: 35 additions & 0 deletions contributions/jaxbCompositionJava10/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apply plugin: 'java'
apply plugin: 'eclipse'

group = 'org.softlang.company'
version = '1.0.0'

// Implementation specifics
// This implementation uses JUnit for testing
// Added JAXB Dependencies, which are deprecated in JDK9+
dependencies {
compile('javax.activation:activation:1.1')
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.0-b180830.0359'
compile group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: '2.4.0-b180830.0438'
testCompile group: 'junit', name: 'junit', version: '4.11+'
}
// Generate model classes with xjc
task xjc(type: Exec) {
commandLine 'xjc'
args = ['inputs/Company.xsd', '-d', 'src/main/java', '-p' , 'org.softlang.company.xjc']
doFirst {
mkdir 'src/main/java/org/softlang/company/xjc'
}
}
// Execute xjc before compiling Java code
compileJava {
dependsOn 'xjc'
}
// Additional cleanup for outputs and generated files
clean {
dependsOn cleanEclipse
doFirst {
delete 'outputs'
delete 'src/main/java/org/softlang/company/xjc'
}
}
35 changes: 35 additions & 0 deletions contributions/jaxbCompositionJava10/inputs/Company.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns="http://www.company.softlang.org/company.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://www.company.softlang.org/company.xsd">

<xs:element name="company">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="department"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="department">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element name="manager" type="employee"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="department"/>
<xs:element maxOccurs="unbounded" minOccurs="0" name="employee" type="employee"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name="employee">
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="address"/>
<xs:element ref="salary"/>
</xs:sequence>
</xs:complexType>

<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="salary" type="xs:double"/>
</xs:schema>
55 changes: 55 additions & 0 deletions contributions/jaxbCompositionJava10/inputs/sampleCompany.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8" ?>
<company
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.company.softlang.org/company.xsd" >
<name>ACME Corporation</name>

<department>
<name>Research</name>
<manager>
<name>Craig</name>
<address>Redmond</address>
<salary>123456</salary>
</manager>
<employee>
<name>Erik</name>
<address>Utrecht</address>
<salary>12345</salary>
</employee>
<employee>
<name>Ralf</name>
<address>Koblenz</address>
<salary>1234</salary>
</employee>
</department>

<department>
<name>Development</name>
<manager>
<name>Ray</name>
<address>Redmond</address>
<salary>234567</salary>
</manager>
<department>
<name>Dev1</name>
<manager>
<name>Klaus</name>
<address>Boston</address>
<salary>23456</salary>
</manager>
<department>
<name>Dev1.1</name>
<manager>
<name>Karl</name>
<address>Riga</address>
<salary>2345</salary>
</manager>
<employee>
<name>Joe</name>
<address>Wifi City</address>
<salary>2344</salary>
</employee>
</department>
</department>
</department>
</company>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

package org.softlang.company.features;

import java.util.Optional;

import org.softlang.company.xjc.*;

public class Cut {

public static void cut(Company company) {
Optional.ofNullable(company)
.map(Company::getDepartment)
.ifPresent(depts -> depts.forEach(Cut::cut));
}

public static void cut(Department department) {
Optional.ofNullable(department.getManager())
.ifPresent(Cut::cut);
Optional.ofNullable(department.getDepartment())
.ifPresent(dep -> dep.forEach(Cut::cut));
Optional.ofNullable(department.getEmployee())
.ifPresent(employee -> employee.forEach(Cut::cut));
}

public static void cut(Employee employee) {
employee.setSalary(employee.getSalary() / 2);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.softlang.company.features;

import org.softlang.company.xjc.Company;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class Serialization {

private static JAXBContext jaxbContext;

public static void initializeJaxbContext()
throws JAXBException
{
if (jaxbContext==null)
jaxbContext =
JAXBContext.newInstance("org.softlang.company.xjc");
}

public static Company deserializeCompany(File input)
throws JAXBException
{
initializeJaxbContext();
return (Company) jaxbContext.createUnmarshaller().unmarshal(input);
}

public static void serializeCompany(File output, Company c)
throws JAXBException,
FileNotFoundException,
XMLStreamException
{
initializeJaxbContext();
XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileOutputStream(output));
jaxbContext.createMarshaller().marshal(c, writer);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

package org.softlang.company.features;

import java.util.Optional;

import org.softlang.company.xjc.*;

public class Total {

public static double total(Company company) {

return company.getDepartment()
.stream()
.map(depts -> total(depts))
.reduce(0.0, Double::sum);
}

public static double total(Department department) {

double total = Optional.ofNullable(department.getManager()).map(Employee::getSalary).orElse(0.0);

total += department.getDepartment()
.stream()
.map(dep -> total(dep))
.reduce(0.0, Double::sum);

return total += department.getEmployee()
.stream()
.map(emp -> emp.getSalary())
.reduce(0.0, Double::sum);
}
}
Loading