Skip to content

Instantly share code, notes, and snippets.

View biniama's full-sized avatar

Biniam biniama

View GitHub Profile
@biniama
biniama / ListItemCounter.groovy
Created August 22, 2016 09:39
How to count elements of List of List (count number of p's in List<Car>)
class Car {
String model
List<Integer> p = new ArrayList<>()
}
List<Car> cars = new ArrayList<Car>()
Car car1 = new Car()
car1.model = "BMW"
car1.p[0] = 1
@biniama
biniama / ex1.java
Last active August 20, 2018 22:55
Java Exam from MUM
1. Write a function that accepts an array of non-negative integers and returns the second largest integer in the array. Return -1 if there is no second largest.
The signature of the function is
int f(int[ ] a)
Examples:
if the input array is return
{1, 2, 3, 4} 3
{{4, 1, 2, 3}} 3
{1, 1, 2, 2} 1
{1, 1} -1
@biniama
biniama / Consumer.java
Last active December 7, 2023 13:46
Producer-Consumer Design Pattern in Java
package com.demo.pc;
import java.util.concurrent.BlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Consumer implements Runnable {
private final BlockingQueue<Integer> sharedQueue;
@biniama
biniama / Query and Output 1
Last active September 16, 2017 10:00
GraphDatabase - Titan with AWS DynamoDB
#A simple Gremlin query that finds restaurants based on a type of cuisine is shown below:
gremlin> g.V.has('genreId', 'Pizzeria').in.restaurant_name
==>La Fontana Pizza Restaurante and Cafe
==>Dominos Pizza
==>Little Cesarz
==>pizza clasica
==>Restaurante Tiberius

Swagger Setup for Embedded Jetty Server

In setting up a Jetty server with Jersey servlets, you may choose to use an embedded Jetty server setup. (See here for how to setup an embedded Jetty server). In this gist, we'll go through how to setup Swagger for this setup. I am using Swagger 1.5, Maven 3.3.3, Jersey 1.8, and Jetty 7.3. Make sure you add all dependencies to your pom.xml.

In the Swagger Core setup, the current official recommendations involve an Application class, or a web.xml, neither of which are used in an embedded Jetty server setup. To add Swagger to your embedded Jetty Server, you must do 3 things:

  1. Add the package scanning packages to your servlets which will serve your REST API.
  2. Add the Swagger package scanning servlet.
  3. P
public class HelloWorld {
public static void main(String [] args) {
System.out.println("Hello, Ese and Haile!");
}
}
//public - accessible from anywhere
//static - only a single instance while executing
//void - does not return anything
//Function(method) = input -> processing -> output
@biniama
biniama / Array.java
Created May 24, 2018 23:07
Array Example
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
/*
Define an array and insert element to an array
@biniama
biniama / PhonePadExercise.java
Last active October 17, 2018 07:44
"Find Unique Numbers of Length n" using Java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PhonePadExercise {
public static void main(String[] args) {
Integer selectedDigit = 4;
@biniama
biniama / SingeltonGoF.java
Last active October 20, 2018 12:07
Singleton Pattern - Multiple Implementations by Allen Holub (holub.com/patterns)
class Singelton {
static Singleton instance = null;
//private constructor
private Singelton() {}
public static Singleton getInstance() {
if(instance == null)
instance = new Singleton();
return instance;
@biniama
biniama / Difference
Last active October 20, 2018 14:25
Adapter vs Decorator vs Bridge
In scope and intent
Adapter - intent is to mix an interface into a class that doesn't currently implement htat interface.
Decorator - intent is to change the way a method behaves differently for different decorators.
Bridge - intent is to insolate sub-systems so you can swap out sub-systems without impacting the code that is using that sub-systems.
The point of a Bridge is to isolate two subsystems so that they can be independently modified. Bridges are big things, often comprised of other patterns (like Adapters and Decorators). This segment looks at how to implement a Bridge and discusses the differences between Bridge and similar patterns like Adapter.
E.g. JDBC (bad implementation of Bridge since swapping doesn't work as it should)
Best example: ORM systems like Hibernate.