Wednesday, 10 June 2015

How to draw an html table with diagonal lines and diagonal text?

The main building blocks are:
  • A nicely marked up <table>
  • A pseudo element border with skew to get the basic shape
  • A rotate on the span that contains the header text.
The concept is pretty basic, the pseudo elements and spans are positioned using position: absolute and they are positioned relative to their parent <th> headers which have position: relative
Here is the example!
Here is what it looks like in Chrome / Firefox / IE10+. IE 8 - 9 should work with their proprietary filters.
Screenshot
Here is the HTML / CSS:
* {
    margin: 0;
    padding: 0;
}

body {
    background: #FFF;
}
table {
    border-collapse: collapse;
    margin: 50px 0 0 50px;
    border-top: solid 1px #000;
    position: relative;
}

/* Very top border */
 table:before {
    content:'';
    display: block;
    position: absolute;
    top: -20px;
    left: 120px;
    height: 20px;
    width: 240px;
    border: solid 1px #000;
    border-bottom: none;
}

/* Far right headers top border (it's outside the table) */
 table:after {
    content:'';
    display: block;
    position: absolute;
    border-top: solid 1px #000;
    width: 101px;
    right: -101px;
    top: 0;
}

/* 
     - Apply header background/font colour 
     - Set base z-index for IE 9 - 10
*/
 thead, th:before {
    background: #03a9f4;
    color: #FFF;
    z-index: 1;
}

/* min-width and max-width together act like a width */
 th {
    min-width: 60px;
    max-width: 60px;
    position: relative;
    height: 100px;
}

/* Pseudo element borders */
 th:before {
    content:'';
    display: block;
    position: absolute;
    top: 0;
    right: -50px;
    height: 100px;
    width: 60px;
    border: solid 1px #000;
    border-right: none;
    border-top: none;
    transform: skew(-45deg);
    border-bottom: none;
}

/* Apply the right border only to the last pseudo element */
 thead th:last-child:before {
    border-right: solid 1px #000;
}

/* Apply the top border only to the first rows cells */
 tbody tr:first-child td {
    border-top: solid 1px #000;
}

/* 
     - Rotate and position headings
     - Padding centers the text vertically and does the job of height
     - z-index ensures that the text appears over the background color in IE 9 - 10
*/
 th span {
    transform: rotate(-45deg);
    display: inline-block;
    vertical-align: middle;
    position: absolute;
    right: -70px;
    bottom: 29px;
    height: 0;
    padding: 0.75em 0 1.85em;
    width: 100px;
    z-index: 2;
}

/* Create first two th styles */
 th:nth-child(1), th:nth-child(2) {
    border: solid 1px #000;
    border-top: none;
    border-bottom: none;
}
th:nth-child(2) {
    border-right: none;
}
th:nth-child(1):before, th:nth-child(2):before {
    display: none;
}

td {
    border: solid 1px #000;
    border-bottom: none;
    border-top: none;
    height: 20px;
    text-align: center;
}
tfoot {
    border: solid 1px #000;
}
 
HTML Code :  
 
<table>
    <thead>
        <tr>
            <th>One</th>
            <th>Two</th>
            <th><span>Three</span></th>
            <th><span>Four</span></th>
            <th><span>Five</span></th>
            <th><span>Six</span></th>
            <th><span>Seven</span></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>0</td>
            <td>0</td>
            <td>0</td>
            <td>0</td>
            <td>0</td>
            <td>0</td>
            <td>0</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td>0</td>
            <td>0</td>
            <td>0</td>
            <td>0</td>
            <td>0</td>
            <td>0</td>
            <td>0</td>
        </tr>
    </tfoot>
</table>

Create diagonal border of a cell






HTML :

<div id="square">
      <span id="room">ROOM TYPE</span>
       <span id="hosp">HOSPITALITY</span></div>
<div id="line"></div>


CSS :

#square {
    position: absolute;
    top: 100px;
    left: 100px;
    background: #3b2a32;
    border: 2px solid #ad1b59;
    color: #ad1b59;
    height: 200px;
    width: 400px;
    font-family: arial, verdana;
    font-size: 22px;
    font-weight: bolder;
}

#room {
    display: block;
    position: absolute;
    top: 50px;
    left: 220px;
}

#hosp {
    display: block;
    position: absolute;
    top: 140px;
    left: 50px;
}


#line {
    position: absolute;
    top: 190px;
    left: 33px;
    height: 200px;
    width: 450px;
    border-top: 2px solid #ad1b59;   
    -webkit-transform: rotate(26.5deg);
       -moz-transform: rotate(26.5deg);
        -ms-transform: rotate(26.5deg);
         -o-transform: rotate(26.5deg);
            transform: rotate(26.5deg);
}

Thursday, 4 June 2015

HashMap with multiple values under the same key


  1. Use a map that has a list as the value. Map<KeyType, List<ValueType>>.
  2. Create a new wrapper class and place instances of this wrapper in the map. Map<KeyType, WrapperType>.
  3. Use a tuple like class (saves creating lots of wrappers). Map<KeyType, Tuple<Value1Type, Value2Type>>.
  4. Use mulitple maps side-by-side.

Examples

1. Map with list as the value
// create our map
Map<string, List<Person>> peopleByForename = new HashMap<string, List<Person>>();    

// populate it
List<Person> people = new ArrayList<Person>();
people.Add(new Person("Bob Smith"));
people.Add(new Person("Bob Jones"));
peopleByForename.Add("Bob", people);

// read from it
List<Person> bobs = peopleByForename["Bob"];
Person bob1 = bobs[0];
Person bob2 = bobs[1];
2. Using wrapper class
// define our wrapper
class Wrapper {
    public Wrapper(Person person1, Person person2) {
       this.person1 = person1;
       this.person2 = person2;
    }

    public Person getPerson1 { return this.person1; }
    public Person getPerson2 { return this.person2; }

    private Person person1;
    private Person person2;
}

// create our map
Map<string, Wrapper> peopleByForename = new HashMap<string, Wrapper>();

// populate it
Wrapper people = new Wrapper()
peopleByForename.Add("Bob", new Wrapper(new Person("Bob Smith"),
                                        new Person("Bob Jones"));

// read from it
Wrapper bobs = peopleByForename["Bob"];
Person bob1 = bobs.Person1;
Person bob2 = bobs.Person2;
3. Using a tuple
// you'll have to write or download a Tuple class in Java, (.NET ships with one)

// create our map
Map<string, Tuple2<Person, Person> peopleByForename = new HashMap<string, Tuple2<Person, Person>>();

// populate it
peopleByForename.Add("Bob", new Tuple2(new Person("Bob Smith",
                                       new Person("Bob Jones"));

// read from it
Tuple<Person, Person> bobs = peopleByForename["Bob"];
Person bob1 = bobs.Item1;
Person bob2 = bobs.Item2;
4. Multiple maps
// create our maps
Map<string, Person> firstPersonByForename = new HashMap<string, Person>();
Map<string, Person> secondPersonByForename = new HashMap<string, Person>();

// populate them
firstPersonByForename.Add("Bob", new Person("Bob Smith"));
secondPersonByForename.Add("Bob", new Person("Bob Jones"));

// read from them
Person bob1 = firstPersonByForename["Bob"];
Person bob2 = secondPersonByForename["Bob"];

Underscore JS


JS File : underscore.js

Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects. It’s the answer to the question: “If I sit down in front of a blank HTML page, and want to start being productive immediately, what do I need?” … and the tie to go along with jQuery's tux and Backbone's suspenders.


Usage :

var groupedData = _.groupBy(data, function(d){return d.division});
console.log(groupedData);


var data = [{
    "division": "East",
    "rm_name": "Russ Martin",
    "address": "MT,VT, NH, ME  (all firms)",
    "state": "MT",
    "coordinates": "43.299428,-74.217933"
}, {
    "division": "East",
    "rm_name": "Carey Fischer",
    "address": "NY- Upstate ex Rockland County (BD, FP)",
    "state": "NY",
    "coordinates": "46.879682,-110.362566"
}, {
    "division": "East",
    "rm_name": "Brandon Born",
    "address": "CT, NY - Upstate, MA - ex metro Boston (FI), MA - Central, West (all firms)",
    "state": "CT",
    "coordinates": "40.714353,-74.005973"
}, {
    "division": "East",
    "rm_name": "Joe Tocyloski",
    "address": "PA - East, NJ - South (FP)",
    "state": "PA",
    "coordinates": "41.603221,-73.087749"
}, {
    "division": "East",
    "rm_name": "Phil Hemery",
    "address": "NJ (FI), NJ - Bergen County (all firms), NY - NYC (FI,RIA)/Westchester (FI,BD)/Rockland County (all firms)",
    "state": "NJ",
    "coordinates": "41.203456,-77.189941"
}, {
    "division": "East",
    "rm_name": "Bob Mancini",
    "address": "MA - East (all firms)",
    "state": "MA",
    "coordinates": "42.407235,-71.383667"
}, {
    "division": "East",
    "rm_name": "Damien Ramondo",
    "address": "NJ, PA East",
    "state": "NJ",
    "coordinates": "41.203456,-77.189941"
}, {
    "division": "East",
    "rm_name": "Kevin Gang",
    "address": "W VA,PA - West (all firms), OH - Cleveland (BD, FI)",
    "state": "VA",
    "coordinates": "44.314844,-85.602364"
}, {
    "division": "East",
    "rm_name": "Andrew Fischer",
    "address": "MI - (all firms)",
    "state": "MI",
    "coordinates": "40.057052,-74.404907"
}, {
    "division": "East",
    "rm_name": "David Saslowsky",
    "address": "NYC",
    "state": "NY",
    "coordinates": "46.879682,-110.362566"
}, {
    "division": "East",
    "rm_name": "Robert Brazofsky",
    "address": "NYC",
    "state": "NY",
    "coordinates": "46.879682,-110.362566"
}, {
    "division": "East",
    "rm_name": "Joseph Proscia",
    "address": "NJ - North ex Bergen County, NY - NYC (FP)",
    "state": "NJ",
    "coordinates": "41.203456,-77.189941"
}, {
    "division": "East",
    "rm_name": "William Marsalise",
    "address": "NY - LI and Outer Boroughs (FI), LI Planners",
    "state": "NY",
    "coordinates": "46.879682,-110.362566"
}, {
    "division": "East",
    "rm_name": "Dan Stack",
    "address": "OH - Columbus and South (all firms), KY - Covington (all firms)",
    "state": "OH",
    "coordinates": "37.439974,-78.662109"
}, {
    "division": "East",
    "rm_name": "James Broderick",
    "address": "OH - North of Columbus ex Cleveland (all firms), Cleveland (FP)",
    "state": "OH",
    "coordinates": "37.439974,-78.662109"
}, {
    "division": "South",
    "rm_name": "Chris Carrelha",
    "address": "FL - North (FP)",
    "state": "FL",
    "coordinates": "40.417287,-82.907123"
}, {
    "division": "South",
    "rm_name": "Don Connell",
    "address": "TN, AR, KY - ex Covington (all firms), MO - St. Louis (by firm)",
    "state": "TN",
    "coordinates": "27.664827,-81.515754"
}, {
    "division": "South",
    "rm_name": "Jay O'Connor",
    "address": "NC - ex coast, SC, GA - Savannah to Augusta (all firms)",
    "state": "NC",
    "coordinates": "35.517491,-86.580447"
}, {
    "division": "South",
    "rm_name": "Dwight Cornell",
    "address": "FL - South, VI, PR (all firms)",
    "state": "FL",
    "coordinates": "40.417287,-82.907123"
}, {
    "division": "South",
    "rm_name": "Eric Indovina",
    "address": "AL (all firms), GA (BD, RIA)",
    "state": "AL",
    "coordinates": "14.97198,19.753418"
}, {
    "division": "South",
    "rm_name": "Russ Corby",
    "address": "TX - DFW (BD, FP) Houston Austin and San Antonio (FP, FI)",
    "state": "TX",
    "coordinates": "32.318231,-86.902298"
}, {
    "division": "South",
    "rm_name": "Chris Boeker",
    "address": "TX - Southeast (BD), MS, LA - South (all firms)",
    "state": "TX",
    "coordinates": "32.318231,-86.902298"
}, {
    "division": "South",
    "rm_name": "Robert Nelms",
    "address": "VA - ex DC metro (all firms), NC - Coast, MD (ex Baltimore), VA, DC (FI), TN - northeastern corner (all firms)",
    "state": "VA",
    "coordinates": "44.314844,-85.602364"
}, {
    "division": "South",
    "rm_name": "Joe Dominguez",
    "address": "FL - North (BD, FI)",
    "state": "FL",
    "coordinates": "40.417287,-82.907123"
}, {
    "division": "South",
    "rm_name": "Marc Della Pia",
    "address": "MD, DC - Metro, VA - Alexandria  (BD, FA), Baltimore (FI)",
    "state": "MD",
    "coordinates": "31.968599,-99.901813"
}, {
    "division": "South",
    "rm_name": "Chris Carrelha",
    "address": "GA (FI, FP)",
    "state": "GA",
    "coordinates": "32.166313,-82.902832"
}, {
    "division": "South",
    "rm_name": "Terry Harris",
    "address": "OK, TX - by city (all firms)",
    "state": "OK",
    "coordinates": "32.166313,-82.902832"
}, {
    "division": "West",
    "rm_name": "Arend Elston",
    "address": "AR, MO, IL - South(all firms)",
    "state": "AR",
    "coordinates": "35.007752,-97.092877"
}, {
    "division": "West",
    "rm_name": "John Schmidt",
    "address": "IL - Chicago Metro (BD)",
    "state": "IL",
    "coordinates": "35.20105,-91.831833"
}, {
    "division": "West",
    "rm_name": "Jason Stevens",
    "address": "MN, ND (All Firms)",
    "state": "MN",
    "coordinates": "46.7248,-94.680176"
}, {
    "division": "West",
    "rm_name": "Laura Channell",
    "address": "IL - Chicago Metro (FI, FP)",
    "state": "IL",
    "coordinates": "35.20105,-91.831833"
}, {
    "division": "West",
    "rm_name": "Patrick Denis",
    "address": "WI (all firms), Upper Peninsula of Michigan",
    "state": "WI",
    "coordinates": "46.729553,-94.6859"
}, {
    "division": "West",
    "rm_name": "Michael Cheskis",
    "address": "IN (all firms), IL - Chicago Metro (By Firm)",
    "state": "IN",
    "coordinates": "43.78444,-88.787868"
}, {
    "division": "West",
    "rm_name": "Dave Mitchell",
    "address": "IA, KS, NE, SD, MO - Kansas City (all firms)",
    "state": "IA",
    "coordinates": "40.271144,-86.132812"
}, {
    "division": "West",
    "rm_name": "Paul Moyer",
    "address": "WA, ID, OR (all firms)",
    "state": "WA",
    "coordinates": "41.877741,-93.098145"
}, {
    "division": "West",
    "rm_name": "Peter Szabo",
    "address": "AK, NV - Reno (all firms), N. CA (BD & FI), San Francisco (FI)",
    "state": "AK",
    "coordinates": "47.751074,-120.740139"
}, {
    "division": "West",
    "rm_name": "Lou Tousignant",
    "address": "N.CA (FP), San Francisco (BD & FP)",
    "state": "CA",
    "coordinates": "64.200841,-149.493673"
}, {
    "division": "West",
    "rm_name": "Matt Malvey",
    "address": "CA - San Diego (all firms)",
    "state": "CA",
    "coordinates": "64.200841,-149.493673"
}, {
    "division": "West",
    "rm_name": "Kevin Dausch",
    "address": "N. CA (BD & FI), San Francisco (FI)",
    "state": "CA",
    "coordinates": "64.200841,-149.493673"
}, {
    "division": "West",
    "rm_name": "Scott Hutton",
    "address": "CO, NM, WY, MT (all firms)",
    "state": "Colorado",
    "coordinates": "40.747164,-74.000566"
}, {
    "division": "West",
    "rm_name": "Brian Buehring",
    "address": "CA - North LA to Santa Barbara, HI (all firms)",
    "state": "CA",
    "coordinates": "64.200841,-149.493673"
}, {
    "division": "West",
    "rm_name": "Robert Forrester",
    "address": "CA - Orange County to San Diego (all firms)",
    "state": "CA",
    "coordinates": "64.200841,-149.493673"
}, {
    "division": "West",
    "rm_name": "Mike Ossmen",
    "address": "CA - Downtown LA, Pasadena, South Bay & Inland Empire (All Firms)",
    "state": "CA",
    "coordinates": "64.200841,-149.493673"
}];

ng-mouseover and leave to toggle item using mouse in angularjs

HTML Code :

<div ng-controller="Ctrl">
    <ul ng-repeat="task in tasks">
        <li ng-mouseover="enableEdit(task)" ng-mouseleave="disableEdit(task)">{{task.name}}
              <span ng-show="task.editable"><a>Edit</a></span>
        </li>
    </ul>
</div>


JS  Funtion :

(function () {
    angular.element(document).ready(function () {
        var app = angular.module('myApp', []);

        app.controller("Ctrl", function ($scope) {
            $scope.tasks = [{
                name: 'foo'
            }, {
                name: 'bar'
            }];

            $scope.enableEdit = function (item) {
                item.editable = true;
            };

            $scope.disableEdit = function (item) {
                item.editable = false;
            };
        });

        angular.bootstrap(document, ['myApp']);
    });


}());

Evaluating a math expression given in string form

With JDK1.6, you can use the built-in Javascript engine.


The Java Scripting functionality is in the javax.script package. This is a relatively small, simple API. The starting point of the scripting API is the ScriptEngineManager class. A ScriptEngineManager object can discover script engines through the jar file service discovery mechanism. It can also instantiate ScriptEngine objects that interpret scripts written in a specific scripting language. The simplest way to use the scripting API is as follows:
  1. Create a ScriptEngineManager object.
  2. Get a ScriptEngine object from the manager.
  3. Evaluate script using the ScriptEngine's eval methods.

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;

public class Test {
  public static void main(String[] args) throws Exception{
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("JavaScript");
    String foo = "40+2";
    System.out.println(engine.eval(foo));
    } 
}

Tuesday, 5 May 2015

Externalizable vs Serializable

  • Externalizable is an interface that enables you to define custom rules and your own mechanism for serialization. Serializable defines standard protocol and provides out of the box serialization capabilities.
  • Externalizable extends Serializable.
  • Implement writeExternal and readExternal methods of the Externalizable interface and create your own contract / protocol for serialization.
  • Saving the state of the supertypes is responsibility of the implementing class.
  • You might have seen in my previouse article on how to customize the default implementation of Serializable. These two methods readExternal and writeExternal (Externalizable) supersedes this customized implementation of readObject and writeObject.
  • In object de-serialization (reconsturction) the public no-argument constructor is used to reconstruct the object. In case of Serializable, instead of using constructor, the object is re-consturcted using data read from ObjectInputStream.
  • The above point subsequently mandates that the Externalizable object must have a public no-argument constructor. In the case of Seriablizable it is not mandatory.
  • Behaviour of writeReplace and readResolve methods are same for both Serializable and Externalizable objects. writeReplace allows to nominate a replacement object to be written to the stream. readResolve method allows to designate a replacement object for the object just read from the stream.
  • In most real time scenarios, you can use Serializable and write your own custom implementation for serialization by providing readObject and writeObject.
  • You may need Externalizable,
    1. If you are not happy with the way java writes/reads objects from stream.
    2. Special handling for supertypes on object construction during serialization.