Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fawad Rafi Submission #265

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
5 changes: 2 additions & 3 deletions java/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


apply plugin: 'java'

sourceCompatibility = 1.8
Expand All @@ -11,7 +9,8 @@ repositories {
}

dependencies {
testCompile 'junit:junit:4.12'
compile 'org.springframework.boot:spring-boot-starter:1.2.7.RELEASE'
testCompile 'junit:junit:4.12'
}

task wrapper(type: Wrapper) {
Expand Down
63 changes: 41 additions & 22 deletions java/src/main/java/com/elsevier/education/Exercise1.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,61 @@
package com.elsevier.education;

import java.util.HashSet;
import java.util.Set;

/**

TODO: Make this class immutable.

*/
public class Exercise1 {

public static class Person {

private Set<String> phoneNumbers;
private String firstName;
private String lastName;

public Person() {
/**
* Prevent the class from being extended
* @author frafi
*
*/
public final static class Person {
/**
* All member fields should be marked final so they can only be
* modified inside a constructor.
*/
private final Set<String> phoneNumbers;
private final String firstName;
private final String lastName;

/**
* Remove no-arguments constructor and instead provide a parameterized constructor
*
*/
public Person(String firstName, String lastName, Set<String> phoneNumbers) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumbers = phoneNumbers;
}

/**
* Provider getter only for phone numbers collection, no setter.
* Also provide a deep copy of the phone numbers.
* @return Collection of unique phone numbers
*/
public Set<String> getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(Set<String> newPhoneNumbers) {
phoneNumbers = newPhoneNumbers;
HashSet<String> retVal = new HashSet<String>(phoneNumbers.size());
for(String phone : phoneNumbers) {
retVal.add(new String(phone));
}
return retVal;
}

/**
* Provider getter only for first name, no setter
* @return First Name
*/
public String getFirstName() {
return firstName;
}
public void setFirstName(String newName) {
firstName = newName;
}

/**
* Provider getter only for last name, no setter
* @return Last Name
*/
public String getLastName() {
return lastName;
}
public void setLastName(String newName) {
lastName = newName;
}
}
}
49 changes: 39 additions & 10 deletions java/src/main/java/com/elsevier/education/Exercise2.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.elsevier.education;

/**
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Qualifier;

TODO refactor the Car to use dependency injection of the engine
TODO allow use of either a gas engine or electric engine (create an appropriate abstraction)
TODO make sure we have no-op implementations of the gas engine and electric engine

*/
public class Exercise2 {

public static class Car {

private GasEngine engine = new GasEngine();
/**
* Automatically wire the engine using type (Interface Engine).
* Use a qualifier because there may be multiple classes implementing that interface.
*/
@Autowired
@Qualifier("gasEngine")
private Engine engine;

public Car() {
}
Expand All @@ -20,8 +22,35 @@ public void moveForward() {
engine.spinWheels();
}
}

public static class GasEngine {

/**
* Declare interface for all engines. This allows us to depend on an interface instead of a class.
*/
public static interface Engine {
void spinWheels();
}

/**
* Gas engine implements Engine interface.
* Assign unique name to this implementation. Change qualifier to electricEngine if you want to use a different engine.
*/
@Component
@Qualifier("gasEngine")
public static class GasEngine implements Engine {
public void spinWheels() {
// no-op for now
}
}

/**
* Electric Engine also implements Engine interface.
* Assign unique name to this implementation.
* @author frafi
*
*/
@Component
@Qualifier("electricEngine")
public static class EletricEngine implements Engine {
public void spinWheels() {
// no-op for now
}
Expand Down
16 changes: 5 additions & 11 deletions java/src/main/java/com/elsevier/education/Exercise3.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
package com.elsevier.education;

import java.util.*;

/**

TODO Examine the failing test case for this class.
We should be able to call people.add() twice but end with only one object in it.
We can test with "gradlew test"

*/
public class Exercise3 {

public static class Person {

private static Random generator = new java.util.Random();
private Integer id;

public Person(int newId) {
id = newId;
}

/**
* Hash code should not be unique for every instance of Person.
* It should be depend on internal state and return the same hash for all persons with same ID.
*/
public int hashCode() {
return id * generator.nextInt();
return id;
}

public boolean equals(Object other) {
Expand Down
18 changes: 12 additions & 6 deletions java/src/main/java/com/elsevier/education/Exercise4.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
package com.elsevier.education;

/**

TODO Is Counter thread-safe? If so, why, and if not, how can we fix it?

Counter is not thread-safe. This is because 2 or more threads can simultaneously invoke incremement()
and resetCount() methods that modify the count instance variable.
This can be fixed by converting these 2 methods into synchronized methods.
*/
public class Exercise4 {

public static class Counter {

private int count = 0;

public int increment() {
/**
* Allows only one thread at a time to increment the count
* @return updated counter
*/
public synchronized int increment() {
return ++count;
}

public int getCount() {
return count;
}

public void resetCount() {
/**
* Allows only one thread at a time to reset the count
*/
public synchronized void resetCount() {
count = 0;
}

Expand Down
42 changes: 33 additions & 9 deletions java/src/main/java/com/elsevier/education/Exercise5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
package com.elsevier.education;

/**

TODO: Turn the "Singleton" class into an actual singleton. The main() method should still call .doSomething().

*/
public class Exercise5 {

public static class Singleton {
public void doSomething() {
System.out.println("Doing something....");

/**
* Class-level field that could be accessed by multiple threads concurrently
*/
private static volatile Singleton _instance = null;

/**
* Create private constructor to prevent new operator being invoked on this class
*/
private Singleton() {
}

/**
* Performs lazy-instantiation of singleton instance to conserve resources.
* Prevents multiple threads from simultaneously instantiating the class-level field.
* @return Singleton instance
*/
public static Singleton getInstance() {
if (_instance == null) {
// Lock the entire class in JVM and allow only one thread into this block
synchronized(Singleton.class) {
if (_instance == null) {
_instance = new Singleton();
}
}
}
return _instance;
}

public void doSomeThing() {
System.out.println("Doing something....");
}
}

public static void main(String a[]){
Singleton st = new Singleton();
Singleton st = Singleton.getInstance();
st.doSomeThing();
}
}
13 changes: 13 additions & 0 deletions javascript/SMART-on-FHIR/SmartFHIR/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Editor configuration, see http://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
max_line_length = off
trim_trailing_whitespace = false
39 changes: 39 additions & 0 deletions javascript/SMART-on-FHIR/SmartFHIR/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

# compiled output
/dist
/tmp
/out-tsc

# dependencies
/node_modules

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# misc
/.sass-cache
/connect.lock
/coverage
/libpeerconnection.log
npm-debug.log
yarn-error.log
testem.log
/typings

# System Files
.DS_Store
Thumbs.db
27 changes: 27 additions & 0 deletions javascript/SMART-on-FHIR/SmartFHIR/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SmartFHIR

This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 6.2.5.

## Development server

Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.

## Code scaffolding

Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`.

## Build

Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build.

## Running unit tests

Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).

## Running end-to-end tests

Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/).

## Further help

To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).
Loading