fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args){
  11. new Ideone();
  12. }
  13.  
  14. public Ideone(){
  15. Street s = new Street();
  16. s.name = "Foo";
  17.  
  18. Building b = new Building();
  19. b.number = "1";
  20. b.floors = "1";
  21.  
  22. s.buildings.add(b);
  23.  
  24. b = new Building();
  25. b.number = "2";
  26. b.floors = "2";
  27.  
  28. s.buildings.add(b);
  29.  
  30. b = new Building();
  31. b.number = "3";
  32. b.floors = "1";
  33.  
  34. s.buildings.add(b);
  35.  
  36. for(Building bu : s.buildings){
  37. for(int i = 0; i < Integer.parseInt(bu.floors) * 2; ++i){ //2 apartmen per floor
  38. bu.apartments.add(new Apartment());
  39. }
  40. }
  41. try {
  42. System.out.println(s);
  43. init(s, "buildings.1/floors#3");
  44. System.out.println(s);
  45. init(s, "buildings.2/apartments.1/owner#John");
  46. System.out.println(s);
  47. } catch (Exception ex) {
  48. ex.printStackTrace();
  49. }
  50. }
  51.  
  52. public void init(Object o, String pattern) throws Exception{
  53. String[] array = pattern.split("/");
  54. for(String s : array){
  55. if(s.contains("#")){
  56. String[] param = s.split("\\#");
  57. setValue(o, param[0], param[1]);
  58. } else {
  59. String[] list = s.split("\\.");
  60. o = getListItem(o, list[0], Integer.parseInt(list[1]));
  61. }
  62. }
  63. }
  64.  
  65. public Object getListItem(Object o, String name, int index) throws Exception{
  66. return ((List)o.getClass().getDeclaredField(name).get(o)).get(index);
  67. }
  68.  
  69. public void setValue(Object o, String name, Object value) throws Exception{
  70. o.getClass().getDeclaredField(name).set(o, value);
  71. }
  72. }
  73.  
  74. class Street {
  75. String name;
  76. List<Building> buildings;
  77.  
  78. Street(){ buildings = new ArrayList<Building>(); }
  79.  
  80. @Override
  81. public String toString() {
  82. return "[Street] " + name + " " + buildings;
  83. }
  84. }
  85.  
  86. class Building {
  87. String number;
  88. String floors;
  89. List<Apartment> apartments;
  90.  
  91. Building(){ apartments = new ArrayList<Apartment>(); }
  92.  
  93. @Override
  94. public String toString() {
  95. return "\n\t[Building] " + number + " " + floors + apartments;
  96. }
  97. }
  98.  
  99. class Apartment {
  100. String owner;
  101.  
  102. @Override
  103. public String toString() {
  104. return "[Apartment] " + owner;
  105. }
  106. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
[Street] Foo [
	[Building] 1 1[[Apartment] null, [Apartment] null], 
	[Building] 2 2[[Apartment] null, [Apartment] null, [Apartment] null, [Apartment] null], 
	[Building] 3 1[[Apartment] null, [Apartment] null]]
[Street] Foo [
	[Building] 1 1[[Apartment] null, [Apartment] null], 
	[Building] 2 3[[Apartment] null, [Apartment] null, [Apartment] null, [Apartment] null], 
	[Building] 3 1[[Apartment] null, [Apartment] null]]
[Street] Foo [
	[Building] 1 1[[Apartment] null, [Apartment] null], 
	[Building] 2 3[[Apartment] null, [Apartment] null, [Apartment] null, [Apartment] null], 
	[Building] 3 1[[Apartment] null, [Apartment] John]]