Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Handy Iterable for a range of Integers

Looping a specific number of times produces code a bit too verbose in Java. The JDK5 enhanced for statement is a handy improvement, but you still have to fall back to the traditional for statement if you want to repeat a loop, say, 10 times.

Not with this implementation of Iterable. You can use it like this:

for (int i : new Range(100))
    System.out.printf("I have said this %d times. Read dzone.com!%n", i);

class Range implements Iterable<Integer> {
    
    private final Integer stop;
    
    public Range(int stop) {
        this.stop = stop;
    }

    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            
            private Integer counter = 0;

            public boolean hasNext() {
                return (counter != stop);
            }

            public Integer next() {
                if (counter == stop)
                    throw new NoSuchElementException();
                return ++counter;
            }

            public void remove() {
            }};
    }
    
}

How to create a RAM disk on OS X

32768 = size in sectors (16MB in this case, a sector is 512 bytes)

$ hdid -nomount ram://32768
/dev/disk1
$ newfs_hfs /dev/disk1
$ mkdir /tmp/ramdisk1
$ mount -t hfs /dev/disk1 /tmp/ramdisk1


To unmount:

hdiutil detach /dev/disk1

Change OS X's default screenshot image format

At the prompt, type this:

defaults write com.apple.screencapture type image_format


Replace "image_format" with a file format name, like pdf, png, tiff, etc. Then to take screenshots, Cmd+Shift+4, then drag around the area you want to capture.

MySQL Launchd item for Mac OS X Tiger

(Originally posted at Unquiet)

I had to reinstall Mysql because it wasn’t one of the things I backed up before erasing my hard drive. Since I’m now running Mac OS X 10.4 “Tiger�, I decided to set it up to start when the system boots, but the system for creating startup items has changed slightly. So I saved the following xml in /Library/LaunchDaemons/com.mysql.Mysql.plist:

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC 
         "-//Apple Computer//DTD PLIST 1.0//EN" "
        http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.mysql.Mysql</string>
        <key>OnDemand</key>
        <false/>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/local/mysql/bin/mysqld_safe</string>
        </array>
        <key>ServiceDescription</key>
        <string>Mysql 4.1 Database Server</string>
        <key>UserName</key>
        <string>mysql</string>
        <key>WorkingDirectory</key>
        <string>/usr/local/mysql</string>
    </dict>
    </plist>


Note that I'm using the official OSX distribution of MySQL... but with a few changes to match your database location, you can get this to work with other installs (fink, darwinports, etc).
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS