Shift Operators in Java


References

Quiz

                          // 31                  15                 0
final int i = -1;         //  1111 1111 1111 1111 1111 1111 1111 1111

final int a = (i >>>  1); //  0111 1111 1111 1111 1111 1111 1111 1111
final int b = (i >>> 15); //  0000 0000 0000 0001 1111 1111 1111 1111
final int c = (i >>> 17); //  0000 0000 0000 0000 0111 1111 1111 1111
final int d = (i >>> 31); //  0000 0000 0000 0000 0000 0000 0000 0001

final int e = (i >>> 32); //  ???? ???? ???? ???? ???? ???? ???? ????
assert e == 0; // @@?

Answer

Here is a quote from The Java® Language Specification, Java SE 8 Edition.

If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x1f (0b11111). The shift distance actually used is therefore always in the range 0 to 31, inclusive.

Here are actual values of each right operands evaluated in above code.

decimal hexadecimal binary & 0b11111
 1 0x01 0000 0000 0000 0000 0000 0000 000 0 0001 0b00001 (0x01)
15 0x0F 0000 0000 0000 0000 0000 0000 000 0 1111 0b01111 (0x0F)
17 0x11 0000 0000 0000 0000 0000 0000 000 1 0001 0b10001 (0x11)
31 0x1F 0000 0000 0000 0000 0000 0000 000 1 1111 0b11111 (0x1F)
32 0x20 0000 0000 0000 0000 0000 0000 001 0 0000 0b00000 (0x00)

Do it for yourself

$ cat ShiftTest.java
public class ShiftTest {
    public static void main(final String[] args) {
        System.out.printf("%+d\n", -1 >>> 31);
        System.out.printf("%+d\n", -1 >>> 32);
    }
}
$ javac ShiftTest.java
$ java ShiftTest
+1
-1
$

Singleton Design Pattern


references

double-checked locking

public class MySingleton {

    private static volatile MySingleton INSTANCE = null; // volatile

    public static MySingleton getInstance() {
        if (INSTANCE == null) {                          // check 1
            synchronized (MySingleton.class) {
                if (INSTANCE == null) {                  // check 2
                    INSTANCE = new MySingleton();
                }
            }
        }
        return INSTANCE;
    }

    private MySingleton() {
        super();
    }
}

initialization-on-demand holder idiom

public class MySingleton {

    private static class InstanceHolder {
        private static final MySingleton INSTANCE = new MySingleton(); // final
    }

    public static MySingleton getInstance() {
        return InstanceHolder.INSTANCE;
    }

    private MySingleton() {
        super();
    }
}

Using Google Closure Compiler with Maven Web Application


References

Project Structure

.
|-- pom.xml
`-- src
    `-- main
        |-- java
        |-- resources
        |-- webapp
        |   |-- WEB-INF
        |   |   `-- web.xml
        |   |-- index.jsp
        |   `-- my-app
        |       |-- css
        |       |-- image
        |       `-- js
        |           |-- properties.js // filtering
        |           `-- my.js
        `-- webresources
            |-- his-app
            |   `-- his.interface.js
            |-- her-app-production
            |   |-- css
            |   |-- images
            |   `-- js
            `-- her-app-staging
                |-- css
                |-- images
                `-- js

Web Resources

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <configuration>
    <webResources combine.children="append"> <!-- wow! -->
      <resource>
        <directory>src/main/webapp</directory>
        <filtering>true</filtering>
      </resource>
      <resource>
        <directory>src/main/webresources/his-app</directory>
        <targetPath>his-app</targetPath>
      </resource>
    </webResources>
  </configuration>
</plugin>

Profiling

<profile>
  <id>staging</id>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
          <webResources>
            <resource>
              <directory>src/main/webresources/her-app-staging</directory>
              <targetPath>her-app</targetPath>
            </resource>
          </webResources>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>
<profile>
  <id>production</id>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
          <webResources>
            <resource>
              <directory>src/main/webresources/her-app-production</directory>
              <targetPath>her-app</targetPath>
            </resource>
          </webResources>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>

Closuring

<profile>
  <id>closure</id>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <executions>
          <execution>
            <id>closure-war</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>war</goal>
            </goals>
            <configuration>
              <webXml>${project.basedir}/src/main/webapp/WEB-INF/web.xml</webXml>
              <warSourceIncludes>my-app/js/**/*.js</warSourceIncludes>
            </configuration>
          </execution>
          <execution>
            <id>default-war</id> <!-- !!! overridden !!! -->
            <phase>package</phase>
            <goals>
              <goal>war</goal>
            </goals>
            <configuration>
              <warSourceExcludes>my-app/js/**/*.js</warSourceExcludes>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <id>closure-compile</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <target>
                <taskdef resource="net/sf/antcontrib/antlib.xml"/>
                <property name="fileset.dir"
                         value="${project.build.directory}/${project.artifactId}/mh-app/js"/>
                <property name="closure.class"
                          value="com.google.javascript.jscomp.CommandLineRunner" />
                <property name="compilation.level" value="SIMPLE"/>
                <echo message="fileset.dir: ${fileset.dir}"/>
                <echo message="closure.class: ${closure.class}"/>
                <echo message="compilation.level: ${compilation.level}"/>
                <for param="file" parallel="false">
                  <path>
                    <fileset dir="${fileset.dir}">
                      <include name="**/*.js"/>
                    </fileset>
                  </path>
                  <sequential>
                    <property name="source" value="@{file}"/>
                    <property name="target" value="@{file}.closured"/>
                    <java classname="${closure.class}" classpathref="maven.plugin.classpath"
                          fork="true" failonerror="true">
                      <arg value="--compilation_level"/>
                      <arg value="${compilation.level}"/>
                      <arg value="--js"/>
                      <arg value="${source}"/>
                      <arg value="--js_output_file"/>
                      <arg value="${target}"/>
                    </java>
                    <echo message="moving ${target} to ${source}"/>
                    <move file="${target}" toFile="${source}"/>
                  </sequential>
                </for>
              </target>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>1.0b3</version>
            <exclusions>
              <exclusion>
                <groupId>ant</groupId>
                <artifactId>ant</artifactId>
              </exclusion>
            </exclusions>
          </dependency>
          <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-nodeps</artifactId>
            <version>1.8.1</version>
         </dependency>
          <dependency>
            <groupId>com.google.javascript</groupId>
            <artifactId>closure-compiler</artifactId>
            <version>v20140814</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</profile>

Build

$ mvn -Pproduction,closure clean package
...
[INFO] --- maven-war-plugin:2.4:war (closure-war) @ hpp-web-client-flash ---
...
[INFO] --- maven-antrun-plugin:1.7:run (closure-compile) @ hpp-web-client-flash ---
...
[INFO] --- maven-war-plugin:2.4:war (default-war) @ hpp-web-client-flash ---
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...
$ 

find all jar files and print


I thought I need to print some jar files’ entries in my Java ME SDK’s lib directory.

$ zipinfo -1 cldc_1.8.jar
META-INF/
...
java/util/logging/StreamHandler.class
$

Then I tried to store those output to a text file.

$ zipinfo -1 cldc_1.8.jar > cldc_1.8.jar.txt
$ cat cldc_1.8.jar.txt
META-INF/
...
java/util/logging/StreamHandler.class
$

Then I need to change line delimiters for Windows..

$ unix2dos.exe cldc_1.8.jar.txt
unix2dos: converting file cldc_1.8.jar.txt to DOS format...
$

I realized I can do this in one command.

$ zipinfo -1 cldc_1.8.jar | unix2dos > cldc_1.8.jar.txt
$

And, finally, I can print all jar files entries in a command.

$ for j in *.jar;do zipinfo -1 $j | unix2dos > $j.txt;done
$