source: ether_megapoli/trunk/common/implementation/com/ether/EtherHelper.java @ 475

Last change on this file since 475 was 475, checked in by vmipsl, 12 years ago

BO insertion données _ move files

File size: 7.0 KB
Line 
1package com.ether;
2
3import org.apache.commons.lang.ArrayUtils;
4import org.jetbrains.annotations.NotNull;
5import org.jetbrains.annotations.Nullable;
6
7import java.io.BufferedWriter;
8import java.io.File;
9import java.io.FileWriter;
10import java.io.IOException;
11import java.security.DigestException;
12import java.security.MessageDigest;
13import java.security.NoSuchAlgorithmException;
14import java.util.ArrayList;
15import java.util.Calendar;
16import java.util.List;
17import java.util.Random;
18
19/**
20 * @author vmipsl
21 * @date 09 sept. 2011
22 */
23public abstract class EtherHelper
24{
25    /**
26     * This method returns the list with the firts values of a list of <Pair>
27     *
28     * @param pairList
29     * @return
30     */
31    @NotNull
32    public static <T1, T2> List getFirstValues( @NotNull final List<Pair<T1, T2>> pairList )
33    {
34        final List firstValues = new ArrayList<T1>( pairList.size() );
35        for( final Pair pair : pairList )
36            firstValues.add( pair.getFirstValue() );
37
38        return firstValues;
39    }
40
41    /**
42     * This method returns the list with the second values of a list of <Pair>
43     *
44     * @param pairList
45     * @return
46     */
47    @NotNull
48    public static <T1, T2> List getSecondValues( @NotNull final List<Pair<T1, T2>> pairList )
49    {
50        final List secondValues = new ArrayList<T2>( pairList.size() );
51        for( final Pair pair : pairList )
52            secondValues.add( pair.getSecondValue() );
53
54        return secondValues;
55    }
56
57    public static double[] convertListDoubleToDoubleArray( @NotNull final List<Double> list )
58    {
59        Double[] doubleArray = new Double[list.size()];
60        doubleArray = list.toArray( doubleArray );
61        return ArrayUtils.toPrimitive( doubleArray );
62    }
63
64    @NotNull
65    public static Integer getRandomBetweenTwoInteger( @NotNull final Integer minValue, @NotNull final Integer maxValue )
66    {
67        final Random r = new Random();
68        return minValue + r.nextInt( maxValue - minValue );
69    }
70
71    @NotNull
72    public static Double getRandomBetweenTwoDouble( @NotNull final Double minValue, @NotNull final Double maxValue )
73    {
74        final Random r = new Random();
75        final Double value = minValue + r.nextDouble();
76        if( value > maxValue )
77            return getRandomBetweenTwoDouble( minValue, maxValue );
78        else
79            return value;
80    }
81
82    // TODO : supprimer cette méthode et utiliser JSON pour récupérer pIdPIdList
83    @Nullable
84    public static <T1 extends Object, T2 extends Object> List<Pair<T1, T2>> extractpfIdPIdListFromString( @NotNull final String pfIdPIdArrayString, @NotNull final Class<T1> T1Class, @NotNull final Class<T2> T2Class )
85    {
86        if( pfIdPIdArrayString.isEmpty() )
87            return null;
88
89        final String[] pfIDPIdArray = pfIdPIdArrayString.split( "," );
90        final List<Pair<T1, T2>> pfIdPIdList = new ArrayList<Pair<T1, T2>>( pfIDPIdArray.length );
91        for( final String pfIdPId : pfIDPIdArray )
92        {
93            final String[] pfIdPIdSplit = pfIdPId.split( "-" );
94            final Pair<T1, T2> pfIdPIdPair = new Pair<T1, T2>();
95            if( T1Class.equals( String.class ) )
96                pfIdPIdPair.setFirstValue( (T1) pfIdPIdSplit[0] );
97            else
98                pfIdPIdPair.setFirstValue( (T1) Integer.valueOf( pfIdPIdSplit[0] ) );
99
100            if( T2Class.equals( String.class ) )
101                pfIdPIdPair.setSecondValue( (T2) pfIdPIdSplit[1] );
102            else
103                pfIdPIdPair.setSecondValue( (T2) Integer.valueOf( pfIdPIdSplit[1] ) );
104            pfIdPIdList.add( pfIdPIdPair );
105        }
106        return pfIdPIdList;
107    }
108
109    /**
110     * This method writes the text into the file filePath
111     *
112     * @param filePath
113     * @param text
114     */
115    public static void writeInFile( @NotNull final String filePath, @NotNull final String text )
116    {
117        try
118        {
119            final FileWriter fw = new FileWriter( filePath, true );
120            final BufferedWriter output = new BufferedWriter( fw );
121            output.write( text );
122            output.flush();
123            output.close();
124        }
125        catch( IOException ioe )
126        {
127            ioe.printStackTrace();
128        }
129
130    }
131
132    /**
133     * This method rounded a value with 'n' number after comma
134     *
135     * @param value            : the value to convert
136     * @param numberOfDecimals : the number of decimals after the comma
137     * @return the rounding value
138     */
139    public static Double floor( final Double value, final int numberOfDecimals )
140    {
141        final Double powValue = Math.pow( 10.0, numberOfDecimals );
142        return Math.floor( ( value * powValue ) + 0.5 ) / powValue;
143    }
144
145
146    /**
147     * Returns the specified String as an encrypted hash
148     *
149     * @param stringToEncrypt
150     * @return
151     */
152    @NotNull
153    public static byte[] encryptStringToHash( @NotNull final String stringToEncrypt )
154            throws DigestException, NoSuchAlgorithmException
155    {
156        final byte[] input = stringToEncrypt.getBytes();
157        final MessageDigest digest = MessageDigest.getInstance( "SHA-1" );
158        digest.update( input, 0, input.length );
159        final int hashLength = 20; // SHA-1 donne un hash de longueur 20
160        final byte[] hash = new byte[hashLength];
161        digest.digest( hash, 0, hashLength );
162        return hash;
163    }
164
165    /**
166     * Returns the specified bytes array hash as a String
167     *
168     * @param encryptedHash the hash as a bytes array
169     * @return the hash as a String
170     */
171    @NotNull
172    public static String displayEncryptedHash( @NotNull final byte[] encryptedHash )
173    {
174        final StringBuilder stringBuilder = new StringBuilder();
175        for( final byte aHash : encryptedHash )
176        {
177            final int v = aHash & 0xFF;
178            if( v < 16 )
179                stringBuilder.append( '0' );
180
181            stringBuilder.append( Integer.toString( v, 16 ) );
182        }
183        return stringBuilder.toString();
184    }
185
186    /**
187     * This method creates a encrypted password with SHA-1 hash
188     *
189     * @param password
190     * @return
191     */
192    @NotNull
193    public static String encryptPassword( @NotNull final String password )
194            throws DigestException, NoSuchAlgorithmException
195    {
196        final byte[] sHAPassword = EtherHelper.encryptStringToHash( password );
197        return EtherHelper.displayEncryptedHash( sHAPassword );
198    }
199
200
201    /**
202     * This method move all files from fromDirectory to toDirectory
203     * The toDirectory DOESN'T MUST exist !
204     * (otherwise it is renamed with date)
205     *
206     * @param fromDirectory
207     * @param toDirectory
208     * @return
209     */
210    public static boolean moveFiles( @NotNull final String fromDirectory, @NotNull final String toDirectory )
211    {
212        final File fromDirectoryFiles = new File( fromDirectory );
213        final File toDirectoryFiles = new File( toDirectory );
214
215        if( toDirectoryFiles.exists() )
216            toDirectoryFiles.renameTo( new File( toDirectory + "_" + Calendar.getInstance().getTimeInMillis() ) );
217
218        return fromDirectoryFiles.renameTo( toDirectoryFiles );
219    }
220
221}
Note: See TracBrowser for help on using the repository browser.