Monday, November 18, 2013

== vs equals in java

/**
 *
 * TODO Description go here.
 *
 * @author <a href="mailto:varra@outlook.com">Rajakrishna V. Reddy</a>
 * @version 1.0
 *
 */
public class StringManual
{
   
    /**
     * The main method.
     *
     * @param args
     *            the arguments
     */
    public static void main(String[] args)
    {
        String s1 = "foo"; //Creates a string in String constant pool rather in heap. [Happens the same for all strings created as ""]
        String s2 = "foo"; // String constant pool will not create a new string as there is already a string with the same content S1, so s2 points to the s1 i.e s2=s1.
       
        System.out.println(s1 == s2); // TRUE: because s2=s1, both references are same i.e s1.
        System.out.println(s1.equals(s2)); // TRUE: based on content.
       
        String s3 = new String("foo"); //Creates a string in heap. [Happens the same for all strings created as new String.]
        System.out.println(s1 == s3); // FALSE: because both references are not same i.e s1 is in constant pool and s3 is in heap.
        System.out.println(s1.equals(s3)); // TRUE: based on content.
    }
}

Wednesday, June 19, 2013

Script to calculate the logIn time in linux

This is could be very useful when you need to calculate the logIn time for attendance/swipes. Here it goes.


#!/bin/bash

. ~/.bash_functions

function updateLogInTime()
{
    file=$1
    fullDate=`date +"%d-%m-%Y %H:%M:%S"`
    today=`date +"%d-%m-%Y"`;
    todayEntry=`grep $today $1`;
    now=`date +"%H:%M:%S"`;
    nowInSecs=`tounixtime`
    if [ -z "$todayEntry" ]; then
       echo "UpdatedAt: $fullDate, In: $now/$nowInSecs, Out: $now/$nowInSecs, Total: `date -u -d @"0" +'%-H:%-M:%-S'`" >> $file;
    else
       tokens=( `getTokens "$todayEntry" ,` )
       # use for loop read all fields
       for (( i=0; i<${#tokens[@]}; i++ ))
       do
           token=${tokens[i]}
           case $token in
                UpdatedAt:)
                        updatedTodayEntry="$updatedTodayEntry $token $fullDate";;
                In:)
                        inTimeToken=${tokens[i+1]};
                        inTimeTokens=( `getTokens "$inTimeToken" /` );
                        inTime=${inTimeTokens[1]};
                        updatedTodayEntry="$updatedTodayEntry, $token $inTimeToken";;
                Out:)
                        updatedTodayEntry="$updatedTodayEntry, $token $now/$nowInSecs";;
                Total:)
                        diff=`calc $nowInSecs-$inTime`;
                        updatedTodayEntry="$updatedTodayEntry, $token `date -u -d @"$diff" +'%-H:%-M:%-S'`";;
           esac
       done
       updatedTodayEntry=`trim $updatedTodayEntry`
       sed -i s%"${todayEntry}"%"${updatedTodayEntry}"%g $file
    fi
}

function main()
{
     screenLocked=`isScreenLocked`;
     if [ $screenLocked -eq 0 ]; then
        updateLogInTime /krishna/Misc/Notifications/data/logInTime.db
     fi
}

main

Happy Coding!

Thursday, April 4, 2013

Installing application sources jar to local maven repo.

I've been searching for a way to install my own application sources to my local maven repo along with the jar file, but couldn't found any where and not even in maven site.

But finally I got a way to do it when I was analyzing the maven plugins. Here it goes, just have this entry in ur pom.xml!!

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1.2</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
 </build>

Fun to be coding!