<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: switch code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 06:12:02 GMT</pubDate>
    <description>DZone Snippets: switch code</description>
    <item>
      <title>Treating first/last loop iterations differently</title>
      <link>http://snippets.dzone.com/posts/show/4353</link>
      <description>Sometimes, you need to iterate over a list of items and treat the first and last element differently from the rest. This tends to produce really messy code, which can be avoided using the following snippet.&lt;br /&gt;&lt;br /&gt;Imagine you have a list of Kittens, and you want to print out a summary of them, saying "My kittens are called Bob, James, John, and Ally." You need to treat the first and last element of the list differently from the rest, lest you end up with superfluous commas in the text. You also need to deal with the cases of there being only one kitten, or no kittens at all.&lt;br /&gt;&lt;br /&gt;To do this, you can avail yourself of a Counter class that keeps track of where you are in the iteration:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;String kittenDesc = "I have no kittens.";&lt;br /&gt;Counter kc = new Counter(kittens);&lt;br /&gt;for (Kitten k : kittens) switch(kc.next()) {&lt;br /&gt;	case one:   kittenDesc =  "My kitten is called " + k.getName() + "."; break;&lt;br /&gt;	case first: kittenDesc =  "My kittens are called " + k.getName();     break;&lt;br /&gt;	case item:  kittenDesc += ", " + k.getName();                         break;&lt;br /&gt;	case last:  kittenDesc += " and " + k.getName() + ".";                break;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;package com.zarkonnen.util;&lt;br /&gt;&lt;br /&gt;import java.util.Collection;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * Used for keeping track of where you are in a collection/array being iterated over. Use by&lt;br /&gt; * initialising with the collection/array before the loop and embedding a switch on the next() Mode&lt;br /&gt; * value into the loop.&lt;br /&gt; *&lt;br /&gt; * LICENCE: This code is licenced under a BSD licence. Feel free to alter and redistribute.&lt;br /&gt; *&lt;br /&gt; * @author David Stark, http://www.zarkonnen.com&lt;br /&gt; * @version 1.0 (2007-07-11)&lt;br /&gt; */&lt;br /&gt;public class Counter {&lt;br /&gt;	/**&lt;br /&gt;	 * An enumeration of modes identifying where in the collection/array we are.&lt;br /&gt;	 */&lt;br /&gt;	public enum Where {&lt;br /&gt;		/** The only element of an array/collection of size 1. */ one,&lt;br /&gt;		/** The first element. */                                 first,&lt;br /&gt;		/** The last element of the array/collection. */          last,&lt;br /&gt;		/** Any other element somewhere in the middle. */         item&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	private int size;&lt;br /&gt;	private int nextIndex = 0;&lt;br /&gt;	&lt;br /&gt;	/**&lt;br /&gt;	 * @param c A collection to keep track of. If its size changes between now and the&lt;br /&gt;	 * iteration, strange things will happen.&lt;br /&gt;	 */&lt;br /&gt;	public Counter(Collection c) {&lt;br /&gt;		size = c.size();&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	/**&lt;br /&gt;	 * @param a An array to keep track of. If its size changes between now and the iteration,&lt;br /&gt;	 * strange things will happen.&lt;br /&gt;	 */&lt;br /&gt;	public Counter(Object[] a) {&lt;br /&gt;		size = a.length;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	/**&lt;br /&gt;	 * @return A Where enum value for where in the array/collection we now are:&lt;br /&gt;	 * &lt;ul&gt;&lt;br /&gt;	 * &lt;li&gt;&lt;strong&gt;one&lt;/strong&gt; at the only element of an array/collection of size 1&lt;/li&gt;&lt;br /&gt;	 * &lt;li&gt;&lt;strong&gt;first&lt;/strong&gt; at the first element&lt;/li&gt;&lt;br /&gt;	 * &lt;li&gt;&lt;strong&gt;last&lt;/strong&gt; at the last element&lt;/li&gt;&lt;br /&gt;	 * &lt;li&gt;&lt;strong&gt;item&lt;/strong&gt; at any other element&lt;/li&gt;&lt;br /&gt;	 * &lt;/ul&gt;&lt;br /&gt;	 */&lt;br /&gt;	public Where next() {&lt;br /&gt;		return size == 1         ? Where.one   :&lt;br /&gt;		       nextIndex++ == 0  ? Where.first :&lt;br /&gt;		       nextIndex == size ? Where.last  :&lt;br /&gt;		                           Where.item  ;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	/**&lt;br /&gt;	 * @return Which index of the array/collection we're currently at.&lt;br /&gt;	 */&lt;br /&gt;	public int index() {&lt;br /&gt;		return nextIndex == 0 ? 0 : nextIndex - 1;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 25 Jul 2007 10:10:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4353</guid>
      <author>Zarkonnen (David Stark)</author>
    </item>
    <item>
      <title>Reverse switch</title>
      <link>http://snippets.dzone.com/posts/show/2318</link>
      <description>Handy technique if you want to use more complex comparisons other than == in a switch statement.  Much tidier than the elseif equivilant.  &lt;br /&gt;&lt;code&gt;&lt;br /&gt;switch(true) {&lt;br /&gt;&lt;br /&gt;   case $x == "hello":&lt;br /&gt;      // $x is hello&lt;br /&gt;&lt;br /&gt;   case is_numeric($x):&lt;br /&gt;      // $x is a number&lt;br /&gt;&lt;br /&gt;   case $x &lt; 10:&lt;br /&gt;      // $x is less than 10&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 20 Jul 2006 20:22:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2318</guid>
      <author>goldfish ()</author>
    </item>
    <item>
      <title>Console keyboard scheme switcher</title>
      <link>http://snippets.dzone.com/posts/show/1901</link>
      <description>Switches between us and swedish (or any other)&lt;br /&gt;keyboard-scheme in the console. Default is US.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/bin/sh&lt;br /&gt;# Keyboard switcher script&lt;br /&gt;# Written in 2006 by Davor Babic &lt;davorb@gmail.com&gt;&lt;br /&gt;# This software is realeased into public domain&lt;br /&gt;&lt;br /&gt;case $1 in&lt;br /&gt;sv)&lt;br /&gt;    echo "Switching to swedish keyboard layout."&lt;br /&gt;    loadkeys /usr/share/keymaps/i386/qwerty/se-latin1.kmap.gz&lt;br /&gt;    ;;&lt;br /&gt;us)&lt;br /&gt;    echo "Switching to US keyboard layout."&lt;br /&gt;    loadkeys /usr/share/keymaps/i386/qwerty/us.kmap.gz&lt;br /&gt;    ;;&lt;br /&gt;default)&lt;br /&gt;    echo "None selected. Loading US..."&lt;br /&gt;    loadkeys /usr/share/keymaps/i386/qwerty/us.kmap.gz&lt;br /&gt;    ;;&lt;br /&gt;esac&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 12 Apr 2006 04:20:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1901</guid>
      <author>davor (Davor Babic)</author>
    </item>
    <item>
      <title>Switch application to foreground in pys60</title>
      <link>http://snippets.dzone.com/posts/show/814</link>
      <description>Taken from the 'appswitch' module &lt;a href=http://pymbian.sourceforge.net/misc/appswitch-v0.20051019.zip&gt;here&lt;/a&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import appswitch&lt;br /&gt;print appswitch.switch_to_fg(u"Menu")&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Updated (19 Oct 05)&lt;br /&gt;==================&lt;br /&gt;Now it can&lt;br /&gt;- listing running applications&lt;br /&gt;- switching them to foreground/background&lt;br /&gt;- closing/killing apps&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# listing&lt;br /&gt;apps = appswitch.application_list(True) # true = include all&lt;br /&gt;                                       # false = no hidden apps &lt;br /&gt;print apps&lt;br /&gt;&lt;br /&gt;# to background and closing&lt;br /&gt;print appswitch.switch_to_bg(u"TODO")&lt;br /&gt;print appswitch.end_app(u"TODO")&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Mon, 17 Oct 2005 21:06:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/814</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Working with both Text and Canvas</title>
      <link>http://snippets.dzone.com/posts/show/577</link>
      <description>In some applications, you need to write some code&lt;br /&gt;in the input text, while the result is display on&lt;br /&gt;a canvas display. For PC apps, you can just make&lt;br /&gt;a two-pane display.&lt;br /&gt;&lt;br /&gt;On pys60, however, the phone can display only&lt;br /&gt;either a Canvas, a Text or a Listbox at the same time.&lt;br /&gt;You cannot use two-pane interface.&lt;br /&gt;&lt;br /&gt;The following code works around this by switching&lt;br /&gt;between two displays.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from appuifw import *&lt;br /&gt;import e32&lt;br /&gt;sleep = e32.ao_sleep&lt;br /&gt;&lt;br /&gt;t = Text()&lt;br /&gt;c = Canvas()&lt;br /&gt;&lt;br /&gt;running = 1&lt;br /&gt;def quit():&lt;br /&gt;    global running&lt;br /&gt;    running = 0&lt;br /&gt;app.exit_key_handler = quit&lt;br /&gt;&lt;br /&gt;while running:&lt;br /&gt;    app.body = t&lt;br /&gt;    sleep(5)&lt;br /&gt;    # show result&lt;br /&gt;    app.body = c&lt;br /&gt;    c.clear()&lt;br /&gt;    c.text((10, 50), t.get())&lt;br /&gt;    sleep(0.5)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 11 Aug 2005 17:59:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/577</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
