Tuesday, August 14, 2012

SPEL : Selecting and Projecting Collections


.?[] to get sub collection
.^[] to get highest element
.$[] to get lowest element
.![] to get string collection


City.java:
package com.lnn.spel.collection;

public class City {
      private String name = null;
      private String state = null;
      private long population = 0;

      public String getState() {
            return state;
      }

      public void setState(String state) {
            this.state = state;
      }

      public long getPopulation() {
            return population;
      }

      public void setPopulation(long population) {
            this.population = population;
      }

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }
}
Data.java:
package com.lnn.spel.collection;

import java.util.List;

public class Data {
      private List<City> bigCities;
      private City firstBigCity;
      private City lastBigCity;
      private List<String> cityNames;
      private List<String> cityStateNames;
      private List<String> condCityNames;
     
      public List<String> getCondCityNames() {
            return condCityNames;
      }

      public void setCondCityNames(List<String> condCityNames) {
            this.condCityNames = condCityNames;
      }

      public List<String> getCityNames() {
            return cityNames;
      }

      public void setCityNames(List<String> cityNames) {
            this.cityNames = cityNames;
      }

      public List<String> getCityStateNames() {
            return cityStateNames;
      }

      public void setCityStateNames(List<String> cityStateNames) {
            this.cityStateNames = cityStateNames;
      }

      public City getFirstBigCity() {
            return firstBigCity;
      }

      public void setFirstBigCity(City firstBigCity) {
            this.firstBigCity = firstBigCity;
      }

      public City getLastBigCity() {
            return lastBigCity;
      }

      public void setLastBigCity(City lastBigCity) {
            this.lastBigCity = lastBigCity;
      }

      public List<City> getBigCities() {
            return bigCities;
      }

      public void setBigCities(List<City> bigCities) {
            this.bigCities = bigCities;
      }    
}
cities.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="  
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-3.0.xsd">
      <util:list id = "cities">
            <bean class="com.lnn.spel.collection.City" p:name="Chicago"
                  p:state="IL" p:population="2853114" />
            <bean class="com.lnn.spel.collection.City" p:name="Atlanta"
                  p:state="GA" p:population="537958" />
            <bean class="com.lnn.spel.collection.City" p:name="Dallas"
                  p:state="TX" p:population="1279910" />
            <bean class="com.lnn.spel.collection.City" p:name="Houston"
                  p:state="TX" p:population="2242193" />
            <bean class="com.lnn.spel.collection.City" p:name="Odessa"
                  p:state="TX" p:population="90943" />
            <bean class="com.lnn.spel.collection.City" p:name="ElPaso"
                  p:state="TX" p:population="613190" />
            <bean class="com.lnn.spel.collection.City" p:name="Jal" p:state="NM"
                  p:population="1996" />
            <bean class="com.lnn.spel.collection.City" p:name="LasCruces"
                  p:state="NM" p:population="91865" />
      </util:list>
      <bean id="data" class="com.lnn.spel.collection.Data">
            <property name="bigCities" value= "#{cities.?[population gt 100000]}"/>
            <property name="firstBigCity" value="#{cities.^[population gt 100000]}"/>
            <property name="lastBigCity" value="#{cities.$[population gt 100000]}"/>
            <property name="cityNames" value="#{cities.![name]}"/>
            <property name="cityStateNames" value="#{cities.![name+','+state]}"/>
            <property name="condCityNames" value="#{cities.?[population gt 100000].![name+','+state]}"/>
      </bean>
</beans>
Test.java:
package com.lnn.spel.collection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
      public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("cities.xml");
            Data data = context.getBean("data", Data.class);
            for(City city : data.getBigCities()){
                  System.out.println(city.getName());
            }
            System.out.println(data.getFirstBigCity().getName());
            System.out.println(data.getLastBigCity().getName());
            for(String cityName : data.getCityNames()){
                  System.out.println(cityName);
            }
            for(String cityStateName : data.getCityStateNames()){
                  System.out.println(cityStateName);
            }
            for(String condCityName : data.getCondCityNames()){
                  System.out.println(condCityName);
            }
      }
}

No comments:

Post a Comment