Java tutorial
//package com.java2s; /* Copyright (C) 2006 Christian Schneider * * This file is part of Nomad. * * Nomad is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Nomad is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Nomad; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ import java.awt.Rectangle; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { private static Pattern rectanglePattern = null; /** * calling this is not thread safe */ public static Rectangle getRectangleProperty(Map p, Object key, Rectangle defaultValue) { Object value = p.get(key); if (value instanceof Rectangle) return (Rectangle) value; else if (value instanceof String) { Rectangle r = parseRectangle((String) value); if (r != null) return r; } return defaultValue; } public static Rectangle parseRectangle(String s) { if (rectanglePattern == null) rectanglePattern = Pattern.compile("\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s*"); Matcher m = rectanglePattern.matcher(s); if (m.matches()) { return new Rectangle(Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2)), Integer.parseInt(m.group(3)), Integer.parseInt(m.group(4))); } return null; } }