1+ /**
2+ * Copyright 2016 SmartBear Software
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package io .swagger .sample .data ;
18+
19+ import io .swagger .sample .model .Category ;
20+ import io .swagger .sample .model .Pet ;
21+ import io .swagger .sample .model .Tag ;
22+
23+ import java .util .List ;
24+ import java .util .ArrayList ;
25+
26+ public class PetData {
27+ static List <Pet > pets = new ArrayList <Pet >();
28+ static List <Category > categories = new ArrayList <Category >();
29+
30+ static {
31+ categories .add (createCategory (1 , "Dogs" ));
32+ categories .add (createCategory (2 , "Cats" ));
33+ categories .add (createCategory (3 , "Rabbits" ));
34+ categories .add (createCategory (4 , "Lions" ));
35+
36+ pets .add (createPet (1 , categories .get (1 ), "Cat 1" , new String [] {
37+ "url1" , "url2" }, new String [] { "tag1" , "tag2" }, "available" ));
38+ pets .add (createPet (2 , categories .get (1 ), "Cat 2" , new String [] {
39+ "url1" , "url2" }, new String [] { "tag2" , "tag3" }, "available" ));
40+ pets .add (createPet (3 , categories .get (1 ), "Cat 3" , new String [] {
41+ "url1" , "url2" }, new String [] { "tag3" , "tag4" }, "pending" ));
42+
43+ pets .add (createPet (4 , categories .get (0 ), "Dog 1" , new String [] {
44+ "url1" , "url2" }, new String [] { "tag1" , "tag2" }, "available" ));
45+ pets .add (createPet (5 , categories .get (0 ), "Dog 2" , new String [] {
46+ "url1" , "url2" }, new String [] { "tag2" , "tag3" }, "sold" ));
47+ pets .add (createPet (6 , categories .get (0 ), "Dog 3" , new String [] {
48+ "url1" , "url2" }, new String [] { "tag3" , "tag4" }, "pending" ));
49+
50+ pets .add (createPet (7 , categories .get (3 ), "Lion 1" , new String [] {
51+ "url1" , "url2" }, new String [] { "tag1" , "tag2" }, "available" ));
52+ pets .add (createPet (8 , categories .get (3 ), "Lion 2" , new String [] {
53+ "url1" , "url2" }, new String [] { "tag2" , "tag3" }, "available" ));
54+ pets .add (createPet (9 , categories .get (3 ), "Lion 3" , new String [] {
55+ "url1" , "url2" }, new String [] { "tag3" , "tag4" }, "available" ));
56+
57+ pets .add (createPet (10 , categories .get (2 ), "Rabbit 1" , new String [] {
58+ "url1" , "url2" }, new String [] { "tag3" , "tag4" }, "available" ));
59+ }
60+
61+ public Pet getPetById (long petId ) {
62+ for (Pet pet : pets ) {
63+ if (pet .getId () == petId ) {
64+ return pet ;
65+ }
66+ }
67+ return null ;
68+ }
69+
70+ public List <Pet > findPetByStatus (String status ) {
71+ String [] statues = status .split ("," );
72+ List <Pet > result = new java .util .ArrayList <Pet >();
73+ for (Pet pet : pets ) {
74+ for (String s : statues ) {
75+ if (s .equals (pet .getStatus ())) {
76+ result .add (pet );
77+ }
78+ }
79+ }
80+ return result ;
81+ }
82+
83+ public List <Pet > findPetByTags (String tags ) {
84+ String [] tagList = tags .split ("," );
85+ List <Pet > result = new java .util .ArrayList <Pet >();
86+ for (Pet pet : pets ) {
87+ if (null != pet .getTags ()) {
88+ for (Tag tag : pet .getTags ()) {
89+ for (String tagListString : tagList ) {
90+ if (tagListString .equals (tag .getName ()))
91+ result .add (pet );
92+ }
93+ }
94+ }
95+ }
96+ return result ;
97+ }
98+
99+ public void addPet (Pet pet ) {
100+ if (pets .size () > 0 ) {
101+ for (int i = pets .size () - 1 ; i >= 0 ; i --) {
102+ if (pets .get (i ).getId () == pet .getId ()) {
103+ pets .remove (i );
104+ }
105+ }
106+ }
107+ pets .add (pet );
108+ }
109+
110+ static Pet createPet (long id , Category cat , String name , String [] urls ,
111+ String [] tags , String status ) {
112+ Pet pet = new Pet ();
113+ pet .setId (id );
114+ pet .setCategory (cat );
115+ pet .setName (name );
116+ if (null != urls ) {
117+ List <String > urlObjs = new ArrayList <String >();
118+ for (String urlString : urls ) {
119+ urlObjs .add (urlString );
120+ }
121+ pet .setPhotoUrls (urlObjs );
122+ }
123+ List <Tag > tagObjs = new java .util .ArrayList <Tag >();
124+ int i = 0 ;
125+ if (null != tags ) {
126+ for (String tagString : tags ) {
127+ i = i + 1 ;
128+ Tag tag = new Tag ();
129+ tag .setId (i );
130+ tag .setName (tagString );
131+ tagObjs .add (tag );
132+ }
133+ }
134+ pet .setTags (tagObjs );
135+ pet .setStatus (status );
136+ return pet ;
137+ }
138+
139+ static Category createCategory (long id , String name ) {
140+ Category category = new Category ();
141+ category .setId (id );
142+ category .setName (name );
143+ return category ;
144+ }
145+ }
0 commit comments